call(back)
Algorithms & Data StructureseasySign in to star problems

Run-Length Encode and Decode

Run-length encode a string as each character followed by the length of its run, and decode such a string back. Counts can be more than one digit.

encode("aaabcc")   ->  "a3b1c2"
decode("a3b1c12")  ->  "aaab" + "c" * 12

Source strings contain letters only, so every digit in an encoded string belongs to a count.

Hints

Hint 1

Encoding: from each position, walk forward while the character repeats; emit the character and the run length, then jump to the end of the run.

Hint 2

Decoding: read one character, then build the count digit by digit (count * 10 + digit) until the next letter — counts can be longer than one digit.

Asked at