class Solution:
def decodeString(self, s: str) -> str:
# time O(max(K)N) space O(max(K)N)
# can't use tuple because tuple is immutable
st = [["",1]]
num = ""
for ch in s:
#case 1: number
if ch.isdigit():
num += ch
#case2: [, push "" num to stack, reset num
elif ch == '[':
st.append(["",num])
num = ""
# case3: ], pop and push partial complete string(ch * num) into stack
elif ch.isalpha():
st[-1][0] += ch
elif ch == ']':
c,n = st.pop()
st[-1][0] += c * int(n)
# case4: alphabetic, keep update
return st[0][0]