- Traverse s
- push number to stack num
- push [ & characters to stack char, until meet ]
- when meet ], pop stack char & add characters together until meet [
- pop the stack num, decode characters, push result back to stack char
- return stack char content
def decodeString(self, s):
num = []
char = []
i = 0
while i < len(s):
if s[i].isdigit():
n = ""
while(s[i].isdigit()):
n+=s[i]
i+=1
num.append(int(n))
elif s[i] == ']':
tmp = ""
while char[-1]!='[':
tmp = char.pop()+tmp
char.pop()
tmp = tmp*num.pop()
char.append(tmp)
i+=1
else:
char.append(s[i])
i+=1
res = ""
while len(char)>0:
res = char.pop()+res
return res
Complexity Analysis
- Time Complexity:O(n)+O(n)+O(n)=O(n)
- Space Complexity:O(n)