Run-Length Decode a String

Medium ⏱ 12 min 60% acceptance ★★★★★ 4.5
Write run_length_decode(s) that reverses run-length encoding: given a string of character + count pairs (counts may be more than one digit, e.g. "a12b1"), expand it back into the original repeated-character string.

Examples

Example 1
Input
s = 'a3b2c1'
Output
'aaabbc'
Explanation

'a' x3, 'b' x2, 'c' x1, concatenated.

Example 2
Input
s = 'a12b1'
Output
'aaaaaaaaaaaab'
Explanation

The count can span multiple digits (12), so digit-scanning must not stop after one character.

Constraints

  • s is a valid run-length-encoded string: one letter followed by one or more digits, repeated.

Topics

StringsString Compression

Companies

AmazonFlipkart

Hints

Hint 1

Walk through s: read one character, then keep consuming digits with `str.isdigit()` until you hit the next letter.

Hint 2

`int(...)` the accumulated digit string to get the repeat count.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.