Problem 258630 · medium · Phase 02 Linear Data Structures

Expand Run Counts

strings · compression · parsing

A string was compressed by writing each run of equal characters as an optional count followed by the character; a count of 1 was omitted ("aaabcc" became "3ab2c"). Write rle_decode(s) that reverses this and returns the original string. Counts may have several digits, and a count of 0 produces nothing.

Examples

Input:  s = "3ab2c"
Output: "aaabcc"

Input:  s = "12x"
Output: "xxxxxxxxxxxx"
Explanation: the count is twelve, not "1" then "2".

Input:  s = "abc"
Output: "abc"

Constraints

  • 0 <= len(s) <= 10**4, the characters being repeated are never digits
  • The decoded string has at most 10**5 characters
  • Target: O(output length)

Goals

  • Accumulate a multi-digit number while scanning
  • Distinguish an omitted count from an explicit one
  • Reset parser state after each character is emitted
Starting Python…