from collections import deque
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ""
st, end = [], 0
while end < len(s):
if s[end].isdigit():
num = 0
while end < len(s) and s[end].isdigit():
num = num*10 + int(s[end])
end += 1
st.append(num)
elif s[end] == "[":
st.append("[")
end += 1
elif s[end].isalpha():
temp = []
while end < len(s) and s[end].isalpha():
temp.append(s[end])
end += 1
st.append("".join(temp))
elif s[end] == "]":
x = deque()
while st[-1] != "[":
x.appendleft(st[-1])
st.pop()
st.pop()
if st and type(st[-1]) is int:
temp_str = "".join(x) * st[-1]
st.pop()
st.append(temp_str)
end += 1
return "".join(st)