Compress a lowercase string by scanning it left to right and replacing every maximal run of the same character with that character followed by the length of the run.
For example the runs of aaab are aaa and b, producing a3b1. The count is always written, even when it is 1.
One line: a non-empty lowercase string s.
One line: the run-length compressed string.
s consists of lowercase English letters.Example 1
Input
aaabbc
Expected
a3b2c1
Explanation
Runs are 'aaa', 'bb', 'c', giving a3b2c1.
Example 2
Input
abc
Expected
a1b1c1
Explanation
Each character is its own run of length 1, giving a1b1c1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →