-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdecode_string.py
More file actions
51 lines (46 loc) · 1.38 KB
/
Copy pathdecode_string.py
File metadata and controls
51 lines (46 loc) · 1.38 KB
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
class Solution2(object): # Credits - LeetCode, 52ms
def decodeString(self, s):
stack = []
stack.append(["", 1])
num = ""
for ch in s:
if ch.isdigit():
num += ch
elif ch == '[':
stack.append(["", int(num)])
num = ""
elif ch == ']':
st, k = stack.pop()
stack[-1][0] += st*k
else:
stack[-1][0] += ch
return stack[0][0]
class Solution: # Credits - LeetCode, 28ms
def decodeString(self, s: 'str') -> 'str':
def group(a):
num = ''
i = 0
while i < len(a) and a[i] in '0123456789':
num += a[i]
i += 1
num = int(num)
j = i+1
count = 1
while j < len(a) and count > 0:
if a[j] == '[':
count += 1
elif a[j] == ']':
count -= 1
j += 1
return num, i, j-1
res = ''
while s:
cur = s[0]
if cur in '0123456789':
num, i, j = group(s)
res += self.decodeString(s[i+1:j])*num
s = s[j+1:]
else:
res += cur
s = s[1:]
return res