Count-First Run-Length Encoding
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) → StringExamples
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.textcontains only lowercase English letters.- Run lengths are written with all of their decimal digits.