Problem · String

Longest Same-Character Substring

EasyCapital OneFULLTIMEOA

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 <= 100
  • source contains only lowercase English letters.
More Capital One problems
drafts saved locally
public String longestSameCharacterSubstring(String source) {
  // write your code here
}
source"bbacccdbbab"
expected"c3"
sign in to submit