Asked at

Decode String

Medium
Verified
StackString Matching~24 min

Given an encoded string s using the rule k[encoded] (repeat encoded k times, nestable), return the decoded string.

The input arrives as a single string s. Use two stacks: one for repeat counts, one for the string built so far.

Examples

in"3[a]2[bc]"
out"aaabcbc"

a repeats 3 times, bc repeats 2 times.

in"3[a2[c]]"
out"accaccacc"

Inner 2[c] -> cc, so a2[c] -> acc, repeated 3 times.

Constraints

  • 1 ≤ s.length ≤ 30
  • Encoded as k[encoded], with k a positive integer.
  • Input is always valid; no leading zeros on k.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.