-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394.py
More file actions
36 lines (26 loc) · 838 Bytes
/
394.py
File metadata and controls
36 lines (26 loc) · 838 Bytes
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
class Solution:
def decodeString(self, s: str) -> str:
stack = []
# nums_stack = []
for char in s:
if char == "]":
rec_str = ""
rec_str_num = ""
while stack[-1] != "[":
rec_str = stack.pop() + rec_str
stack.pop()
while stack and stack[-1].isdigit():
rec_str_num = stack.pop() + rec_str_num
stack.append(rec_str * int(rec_str_num))
else:
stack.append(char)
return("".join(stack))
# s = "3[a]2[b]"
# s = "3[a2[c]]"
# s = "3[a]2[bc]"
# s = "100[leetcode]"
s = "3[z]2[2[y]pq4[2[jk]e1[f]]]ef"
res_s = "zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef"
sol = Solution()
print(sol.decodeString(s))
print(res_s)