FastPrepRightmost Longest Character Run
Problem · String

Rightmost Longest Character Run

Learn this problem
EasyCapital One logoCapital OneINTERNOA

Problem statement

You are given a string source consisting of lowercase English letters. Find the longest contiguous substring consisting of the same character.

If several substrings of the same maximum length meet this condition, choose the rightmost one. Return a string consisting of the selected character concatenated with its number of occurrences in the longest contiguous substring, which is the length of that substring.

A solution with time complexity no worse than O(source.length^3) will fit within the execution time limit.

Function

rightmostLongestCharacterRun(source: String) → String

Examples

Example 1

source = "bbacccdbbab"return = "c3"
  • There are two contiguous substrings consisting of a, and both have length 1.
  • There are three contiguous substrings consisting of b; two have length 2 and one has length 1.
  • There is one contiguous substring consisting of c, and it has length 3. It is the longest contiguous substring, so the answer is c3.

Example 2

source = "bbaacaa"return = "a2"

There are three different contiguous substrings with length 2. The rightmost one is the final 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 rightmostLongestCharacterRun(String source) {
    // write your code here
}
source"bbacccdbbab"
expected"c3"
checking account