Problem · String
Rightmost Longest Character Run
Learn this problemProblem 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) → StringExamples
Example 1
source = "bbacccdbbab"return = "c3"- There are two contiguous substrings consisting of
a, and both have length1. - There are three contiguous substrings consisting of
b; two have length2and one has length1. - There is one contiguous substring consisting of
c, and it has length3. It is the longest contiguous substring, so the answer isc3.
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 <= 100sourcecontains only lowercase English letters.