Problem · String
Longest Same-Character Substring
You are given a string source consisting of lowercase English letters. Find the longest contiguous substring that consists of one repeated character. If several substrings have the same maximum length, choose the rightmost one.
Return a string made from that character followed by the length of the chosen substring.
Examples
01 · Example 1
source = "bbacccdbbab" return = "c3"
The longest contiguous substring with the same character is ccc, so the answer is c3.
02 · Example 2
source = "bbaacaa" return = "a2"
There are multiple substrings of length 2. The rightmost one is aa, so the answer is a2.
Constraints
1 <= source.length <= 100sourcecontains only lowercase English letters.
More Capital One problems
- Count House Segments After DestructionOA · Seen May 2026
- Count Numbers with Even Number of DigitsOA · Seen May 2026
- Laser Robot Safe PathOA · Seen May 2026
- Cyclic Shift to Strictly Descending ArrayOA · Seen May 2026
- Dynamic Wall Building and Range QueryOA · Seen May 2026
- Queue Check-in Simulation with Capacity LimitOA · Seen May 2026
- Get Function Execution TimeSeen Jan 2025
- Sum Digits Until OneSeen Dec 2024
public String longestSameCharacterSubstring(String source) {
// write your code here
}source"bbacccdbbab"
expected"c3"
sign in to submit