FastPrepCount-First Run-Length Encoding

Count-First Run-Length Encoding

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given a string text, encode each maximal run of equal consecutive characters from left to right.

For every run, append the run length in base 10 followed by the run's character. Include the count 1 for a singleton run. Return the concatenated encoding. Return the empty string when text is empty.

Function

runLengthEncode(text: String) → String

Examples

Example 1

text = "aabbcc"return = "2a2b2c"

Each of a, b, and c forms a run of length two.

Example 2

text = "aaabbcaaaaa"return = "3a2b1c5a"

The two separated runs of a are encoded independently, and the singleton c keeps count 1.

Example 3

text = ""return = ""

An empty input has no runs.

Constraints

  • 0 <= text.length <= 100000.
  • text contains only lowercase English letters.
  • Run lengths are written with all of their decimal digits.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String runLengthEncode(String text) {
    // Write your code here.
}
text"aabbcc"
expected"2a2b2c"
Checking account…