Skip to main content
Medium Stack Medium frequency

Decode String

Open on LeetCode

Approach Summary

Use stack to track (current_string, repeat_count). On ], pop and repeat the current segment.

Full Solution & Approach

Nested repetition like 3[a2[c]] requires tracking two pieces of state as you scan: the string built so far at the current nesting level and the repeat count that governs it. Use a stack of (previous_string, repeat_count) pairs. On a digit, accumulate the full number (digits can be multi-character). On an opening bracket, push the current string and number onto the stack and reset both. On a closing bracket, pop the pair, repeat the current string count times, and append it to the popped previous string — this reconstructs the inner level and splices it into the outer one. Ordinary letters are appended to the current string. Each character is processed once, and each push/pop is O(1), though the output itself can be exponential in the nesting depth, which bounds the total work.

Each character is scanned once and each stack operation is O(1) — but the decoded output can be exponential, so the worst case is O(final output length). The stack holds one pair per nesting level — O(depth).

Solution Code

Solution

def decode_string(s: str) -> str:
    stack = []
    cur = ''
    num = 0
    for ch in s:
        if ch.isdigit():
            num = num * 10 + int(ch)
        elif ch == '[':
            stack.append((cur, num))
            cur, num = '', 0
        elif ch == ']':
            prev, k = stack.pop()
            cur = prev + cur * k
        else:
            cur += ch
    return cur

Edge Cases to Watch

  • Multi-digit repeat counts like 10[a]
  • Deeply nested patterns like 3[a2[b1[c]]]
  • Letters before any bracket — appended normally
  • Empty encoded string

How to Recognize This Pattern

  • Nested repeated patterns
  • k[encoded_string]

Complexity Analysis

Time Complexity

O(n · k)

Space Complexity

O(n)

Tags

String Stack Recursion

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee