Skip to content

Decode String

MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.

A string is encoded with the rule k[text], which means text repeated k times. Encodings can be nested, like 3[a2[c]]. Return the decoded string.

s = "3[a]2[bc]"      →  "aaabcbc"
s = "3[a2[c]]"       →  "accaccacc"
s = "2[abc]3[cd]ef"  →  "abcabccdcdcdef"

Constraints

  • 1 <= s.length <= 30
  • s contains lowercase letters, digits and square brackets.
  • s is always valid; digits only appear as repeat counts, and 1 <= k <= 300.
  • The decoded string is at most 10^5 characters long.