-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_string.py
48 lines (39 loc) · 1.22 KB
/
decode_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
digit = []
stack = []
temp = []
for x in s:
if x.isdigit():
digit.append(x)
continue
elif digit:
number = int("".join(digit))
stack.append(str(number))
digit = []
if x == "[":
continue
if x == "]":
temp = []
while stack and not stack[-1].isdigit():
temp.append(stack.pop())
number = int(stack.pop())
temp = temp * number
for x in temp[::-1]:
stack.append(x)
temp = []
else:
stack.append(x)
while stack:
temp.append(stack.pop())
return ''.join(temp[::-1])
abc = Solution()
print (abc.decodeString("3[a]2[bc]"))
# print (abc.decodeString("3[a2[c]]"))
# print (abc.decodeString("2[abc]3[cd]ef"))
# print (abc.decodeString("3[2[a]2[2[b]c]]"))
# print (abc.decodeString("100[leet]"))