Problem · String
Longest Same-Character Substring
Learn this problemProblem statement
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.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(source.length^3) will fit within the execution time limit.
Function
longestSameCharacterSubstring(source: String) → StringExamples
Example 1
source = "bbacccdbbab"return = "c3"- There are two contiguous substrings consisting of the character
"a", both have a length of1. - There are three contiguous substrings consisting of the character
"b", two have a length of2and one has a length of1. - There is just one contiguous substring consisting of the character
"c", which has a length of3. So, this is the longest contiguous substring and hence the answer is"c3".
Example 2
source = "bbaacaa"return = "a2"There are three different contiguous substrings with a length of 2, so the answer should be the rightmost one — "a2".
Constraints
1 <= source.length <= 100sourcecontains only lowercase English letters.
More Capital One problems
- Compare Counts Around PivotOA · Seen Jul 2026
- Format a Newspaper PageOA · Seen Jul 2026
- Laser Robot Safe PathOA · Seen Jul 2026
- Match Consecutive Word BoundariesOA · Seen Jul 2026
- Reconstruct Landmark JourneyOA · Seen Jul 2026
- Rightmost Longest Character RunOA · Seen Jul 2026
- Track Received Byte RangesOA · Seen Jul 2026
- Alternate String EndsOA · Seen Jul 2026