Problem 232099 · medium · Level 02 Linear Data Structures

Expand Repeat Notation

stacks · parsing · strings

A compressed string uses the notation k[body], meaning body repeated exactly k times. Bodies may contain letters and further k[body] groups; k is a positive integer that may have several digits. Return the expanded string.

Examples

Input:  s = "q2[ab]3[z]"
Output: "qababzzz"

Input:  s = "2[w3[n]v]"
Output: "wnnnvwnnnv"

Input:  s = "ab2[cd]e"
Output: "abcdcde"

Constraints

  • 1 <= len(s) <= 100, s is well-formed
  • The expanded string has at most 10**5 characters
  • Target: O(length of output)

Goals

  • Parse multi-digit numbers while scanning
  • Save the outer context on a stack when entering a bracket and restore it on exit
Starting Python…