VISUALGO//PROBLEMS
decode string · stackmedium
LeetCode #394
input
3
[
a
]
2
[
b
c
]
result
·
·
stack
ELEMENTS
s
Python
def decodeString(s: str) -> str:
    stack = []
    current_str = ""
    current_num = ""

    for ch in s:
        if ch.isdigit():
            current_num += ch
        elif ch == '[':
            stack.append((current_str, int(current_num)))
            current_str = ""
            current_num = ""
        elif ch == ']':
            prev_str, count = stack.pop()
            current_str = prev_str + current_str * count
        else:
            current_str += ch

    return current_str