FastPrepFind the Rightmost Longest Character Run

Find the Rightmost Longest Character Run

Susquehanna International Group (SIG) logoSusquehanna International Group (SIG)● EasyFULLTIMEOA

Problem statement

You are given a string source containing lowercase English letters. Find the longest contiguous run of one letter.

If several runs have the same maximum length, choose the rightmost run. Return its letter followed by its length in decimal, with no space between them. For example, a run of twelve copies of x is written as x12.

Function

longestRun(source: String) → String

Examples

Example 1

source = "bbacccdbbab"return = "c3"

The runs, from left to right, are bb, a, ccc, d, bb, a and b. The unique longest run is ccc, so return c3.

Example 2

source = "bbaacaa"return = "a2"

The runs are bb, aa, c and aa. Three runs have length two. The final aa is the rightmost, so return a2.

Constraints

  • 1 <= source.length <= 100
  • source contains only lowercase English letters.

More Susquehanna International Group (SIG) problems

See Susquehanna International Group (SIG) hiring insights
Saved
public String longestRun(String source) {
    // Return the letter followed by its run length.
}
Checking account…