forked from yennanliu/CS_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode-string.py
292 lines (264 loc) · 8.42 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
394. Decode String
Medium
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Example 1:
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Example 2:
Input: s = "3[a2[c]]"
Output: "accaccacc"
Example 3:
Input: s = "2[abc]3[cd]ef"
Output: "abcabccdcdcdef"
Example 4:
Input: s = "abc3[cd]xyz"
Output: "abccdcdcdxyz"
Constraints:
1 <= s.length <= 30
s consists of lowercase English letters, digits, and square brackets '[]'.
s is guaranteed to be a valid input.
All the integers in s are in the range [1, 300].
"""
# V0
# IDEA : STACK
# NOTE : treat before cases separately
# 1) isdigit
# 2) isalpha
# 3) "["
# 4) "]"
# and define num = 0 for dealing with "100a[b]", "10abc" cases
class Solution:
def decodeString(self, s):
num = 0
string = ''
stack = []
"""
NOTE : we deal with 4 cases
1) digit
2) "["
3) alphabet
4) "]"
NOTE :
we use pre_num, pre_string for dealing with previous result
"""
for c in s:
# case 1) : digit
if c.isdigit():
num = num*10 + int(c)
# case 2) : "["
elif c == "[":
stack.append(string)
stack.append(num)
string = ''
num = 0
# case 3) : alphabet
elif c.isalpha():
string += c
# case 4) "]"
elif c == ']':
pre_num = stack.pop()
pre_string = stack.pop()
string = pre_string + pre_num * string
return string
# V1
# https://leetcode.com/problems/decode-string/discuss/378711/Easy-understand-python-solution-88-beat
# IDEA : STACK
# IDEA :
# -> When meet '[' append the previous num and string to stack,
# -> when meet the ']' pop the previous num and string to calculate.
# -> Notice the num*10 + num is for some case like "100[leetcode]" that num is greater than 10.
class Solution:
def decodeString(self, s):
num = 0
string = ''
stack = []
for c in s:
if c.isdigit():
### NOTICE HERE
# num = num*10 + int(c) is for the greater than 10 cases
# e.g.
# input = 100
# -> when idx=0
# -> num=0*10 + 1
# -> when idx=1
# -> num += 1*10 + 0
# -> when idx=2
# -> num += 10*10 + 0
# -> so num = 100
num = num*10 + int(c)
elif c == "[":
# when c == "["
# then cache string and num
# and init string, num again
stack.append(string)
stack.append(num)
string = ''
num = 0
elif c.isalpha():
string += c
elif c == ']':
# when c == ']',
# pop pre num, pre string
# do pre_string + pre_num and add it back to string
pre_num = stack.pop()
pre_string = stack.pop()
string = pre_string + pre_num * string
return string
### Test case
s=Solution()
assert s.decodeString("3[a]2[bc]")=="aaabcbc"
assert s.decodeString("3[a2[c]]")=="accaccacc"
assert s.decodeString("2[abc]3[cd]ef")=="abcabccdcdcdef"
assert s.decodeString("")==""
assert s.decodeString("[]")==""
assert s.decodeString("a")=="a"
assert s.decodeString("abc")=="abc"
assert s.decodeString("[[]]")==""
assert s.decodeString("[[[]]]")==""
assert s.decodeString("[[a]]")==""
assert s.decodeString("[[ab]]")==""
assert s.decodeString("[[[a]]b]")==""
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79332894
# IDEA : STACK
# curstring = prestring + prenum * curstring
# ord() is a way transforming string -> integer
# e.g.
# c = '3' -> ord(c) - ord('0') = 3
# c= '9' -> ord(c) - ord('0') = 9
# we can do that via int() as well. i.e. int('9') -> 9
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
curnum = 0
curstring = ''
stack = []
for char in s:
if char == '[':
stack.append(curstring)
stack.append(curnum)
curstring = ''
curnum = 0
elif char == ']':
prenum = stack.pop()
prestring = stack.pop()
curstring = prestring + prenum * curstring
elif char.isdigit():
curnum = curnum * 10 + int(char)
else:
curstring += char
return curstring
# V1''
# https://leetcode.com/problems/decode-string/discuss/163479/Python-short-and-simple-stack-solution-beats-100-with-explanation
class Solution:
def decodeString(self, s):
stack = []
for i in range(len(s)):
if s[i] == "]":
current = ''
while stack:
val = stack.pop()
if val == "[":
break
current = val + current
num = ''
while stack and stack[-1].isdigit():
num = stack.pop() + num
stack.append(int(num)*current)
else:
stack.append(s[i])
return ''.join(stack)
# V1'''
# https://leetcode-cn.com/problems/decode-string/solution/decode-string-fu-zhu-zhan-fa-di-gui-fa-by-jyd/
class Solution:
def decodeString(self, s: str) -> str:
stack, res, multi = [], "", 0
for c in s:
if c == '[':
stack.append([multi, res])
res, multi = "", 0
elif c == ']':
cur_multi, last_res = stack.pop()
res = last_res + cur_multi * res
elif '0' <= c <= '9':
multi = multi * 10 + int(c)
else:
res += c
return res
# V1''''
# https://yq.aliyun.com/articles/714166
class Solution:
def decodeString(self, s: str) -> str:
#init
stack, res, num = [], '', 0
for c in s:
if c.isdigit():
num = num * 10 + int(c)
elif c.isalpha():
res += c
elif c == '[':
# insert into stack as tuple (res, num )
stack.append((res, num))
# update res and num
res, num = '', 0
else:
# if c == ']', then pop var and repeat times
last_str, this_num = stack.pop()
res = last_str + this_num * res
return res
# V1''''''
# http://bookshadow.com/weblog/2016/09/04/leetcode-decode-string/
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
k = 1
parts = collections.defaultdict(str)
digits = collections.defaultdict(int)
for c in s:
if c.isdigit():
digits[k] = digits[k] * 10 + int(c)
elif c == '[':
k += 1
elif c == ']':
parts[k - 1] += digits[k - 1] * parts[k]
digits[k - 1] = 0
parts[k] = ''
k -= 1
else:
parts[k] += c
return parts[1]
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
curr, nums, strs = [], [], []
n = 0
for c in s:
if c.isdigit():
n = n * 10 + ord(c) - ord('0')
elif c == '[':
nums.append(n)
n = 0
strs.append(curr)
curr = []
elif c == ']':
strs[-1].extend(curr * nums.pop())
curr = strs.pop()
else:
curr.append(c)
return "".join(strs[-1]) if strs else "".join(curr)