FastPrepFastPrep
Problem Brief

Longest Same-Character Substring

FULLTIMEOA

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.

1Example 1

Input
source = "bbacccdbbab"
Output
"c3"
Explanation

The longest contiguous substring with the same character is ccc, so the answer is c3.

2Example 2

Input
source = "bbaacaa"
Output
"a2"
Explanation

There are multiple substrings of length 2. The rightmost one is aa, so the answer is a2.

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= source.length <= 100
  • source contains only lowercase English letters.
public String longestSameCharacterSubstring(String source) {
  // write your code here
}
Input

source

"bbacccdbbab"

Output

"c3"

Sign in to submit your solution.