-
Notifications
You must be signed in to change notification settings - Fork 1
/
lc394_Decode_String.py
171 lines (140 loc) · 4.64 KB
/
lc394_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""
2021.7
idea: string decode problem -> stack or recursion
1. stack: we need to decode string from inner to outer due to brackets
- parse number: carry
- parse string: alphabets
- stack content: num, str, brackets(signs to push and pop element)
"""
# 2021.7 stack
class Solution_stack:
def parse_num(self):
num = 0
while self.i < self.n and self.s[self.i].isdigit() == True:
num = 10 * num + int(self.s[self.i])
self.i += 1
return num
def parse_string(self):
string = ''
while self.i < self.n and self.s[self.i].isalpha() == True:
string += self.s[self.i]
self.i += 1
return string
def decodeString(self, s: str) -> str:
self.i = 0
self.s = s
self.n = len(self.s)
stk = []
while self.i < self.n:
if s[self.i].isdigit() == True:
num = self.parse_num()
stk.append(num)
elif s[self.i].isalpha() == True:
string = self.parse_string()
stk.append(string)
elif s[self.i] == '[':
stk.append('[')
self.i += 1
elif s[self.i] == ']':
self.i += 1
string = ''
while stk[-1] != '[':
string = stk.pop() + string
stk.pop() # 弹出'['
cur_num = stk.pop()
new_string = string * cur_num
stk.append(new_string)
return ''.join(stk)
class Solution_recursion:
def parse_num(self):
num = 0
while self.i < self.n and self.s[self.i].isdigit() == True:
num = 10 * num + int(self.s[self.i])
self.i += 1
return num
def parse_string(self):
string = ''
while self.i < self.n and self.s[self.i].isalpha() == True:
string += self.s[self.i]
self.i += 1
return string
def dfs(self):
string_, multiple = '', 0
while self.i < self.n:
char_ = self.s[self.i]
if char_.isdigit():
multiple = self.parse_num()
elif char_.isalpha():
string_ += self.parse_string()
elif char_ == '[':
self.i += 1
tmp_string = self.dfs()
string_ += multiple * tmp_string
multiple = 0
elif char_ == ']':
self.i += 1
return string_
return string_
def decodeString(self, s: str) -> str:
self.i = 0
self.s = s
self.n = len(self.s)
return self.dfs()
class Solution:
"""
2021.3
assistant stack
- parse number
- parse alphabets
- stk store (current layer string, next layer count) when it comes into '['
- ']': stk pop -> get new string
"""
def decodeString(self, s: str) -> str:
string_ = ''
next_count = 0
stk = []
for char_ in s:
if char_.isdigit() == True:
next_count = 10 * next_count + int(char_)
elif char_.isalpa() == True:
string_ += char_
# push into stack
elif char_ == '[':
stk.append((string_, next_count))
string_, next_count = '', 0
# pop last layer elements
elif char_ == ']':
upper_string, count = stk.pop()
string_ = upper_string + count * string_
return string_
class Solution1:
"""
DFS:
- base case: ']' -> return current idx and current string
- recursive process: decode one layer content in the bracket
"""
def decodeString(self, s: str)->str:
def dfs(i):
res, multiple = '', 0
while i < len(s):
if s[i].isdigit() == True:
multiple = 10 * multiple + int(s[i])
elif s[i].isalpha() == True:
res += s[i]
# start recursion
elif s[i] == '[':
i, tmp = dfs(i+1)
res = res + multiple * tmp
multiple = 0
# complete recursion
elif s[i] == ']':
return i, res
else:
print('Something Wrong!')
i += 1
return res
return dfs(0)
if __name__ == '__main__':
s = "2[abc]3[cd]ef"
S = Solution_recursion()
print(S.decodeString(s))