JavaScriptMediumStrings20 min
Compress consecutive character runs
Replace each maximal run with its character followed by its decimal count, including counts of one. Input contains only lowercase English letters. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess scan each maximal run exactly once.
Input
A JSON string on stdin.
Output
Print one compact JSON value: a string.
Constraints
0 <= string length <= 10000; lowercase English letters only.
Example
Input
"aabccc"
Output
"a2b1c3"
Example 2 input
"abba"
Output
"a1b2a1"
Boundary case: Counts may contain multiple decimal digits
Public test cases (2)
Input
"aabccc"
Expected output
"a2b1c3"
Input
"abc"
Expected output
"a1b1c1"
Hints (2)
- Find the first position with a different character
- Counts may contain multiple decimal digits
Target complexity: time O(n) · space O(n)
