Skip to content
Problem 394 · LeetCode 394(opens in a new tab)

Decode String

MediumRecursion

You get a string s written in a compact code. A group k[text] stands for text written out k times in a row, and groups can sit inside other groups. Letters outside any brackets stand for themselves.

Return the fully expanded string. The input is always well formed: brackets match, every [ comes right after its count, and digits only ever appear as counts.

Example 1

Input:  s = "2[b3[a]]c"
Output: "baaabaaac"
Why:    3[a] is aaa, so the group is baaa, twice, then c
2 more examples and the constraints

Example 2

Input:  s = "3[xy]2[z]"
Output: "xyxyxyzz"
Why:    xy three times, then z twice

Example 3

Input:  s = "ab10[c]"
Output: "abcccccccccc"
Why:    a count can have more than one digit: ten c's

Constraints

  • 1 ≤ s.length ≤ 30
  • s holds lowercase letters, digits and the brackets [ and ]
  • Every count is between 1 and 300
  • The expanded string is at most 100,000 characters long

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Recursive parserO(L)
  2. Stack of saved text and countsO(L)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for an output of 100,000 charactersVerdict
Recursive parserO(L)
Clean; one call per bracket
Stack of saved text and countsO(L)
Best; no recursion, same pass

L = length of the decoded string. Both read `s` once; the cost is writing the output.

Edge cases to test

  • No brackets. "hello" decodes to itself: only the letter branch runs.
  • Counts with several digits. 10[c] and 300[ab] need num = num * 10 + digit, not just the last digit.
  • Deep nesting. 2[2[2[2[k]]]] is 16 k's; each ] doubles what the level below built.
  • Letters after a group. In 2[b3[a]]c the c comes after both groups close and is added once, not repeated.
  • A huge output from a short input. 300[300[a]] is 11 characters but decodes to 90,000. Build with a string builder, not repeated slow concatenation.

Common mistakes

  • Reading only one digit, so 10[c] repeats c once with a stray count.
  • Forgetting to reset current and num after pushing at [.
  • Appending the repeated inside to the wrong text: it goes after the text saved on the stack, not after the inside itself.

How to talk through it

  1. 1Clarify. Is the input always valid? Can counts have several digits? Can digits appear as plain text?
  2. 2Spot the nesting. The inside of a group must be decoded before the outside can use it: a stack or recursion.
  3. 3Describe the state. Keep the current text and the count being read; save both at [ and restore them at ].
  4. 4Code it. One pass with a stack of pairs. O(L) time and space for an output of length L.
  5. 5Test out loud. Try 2[b3[a]]c, a two-digit count, and a string with no brackets.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?