-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeString.py
More file actions
38 lines (38 loc) · 1.15 KB
/
decodeString.py
File metadata and controls
38 lines (38 loc) · 1.15 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
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
if '[' not in s:
return s
res, start, end = '', 0, len(s)
while start < end:
c = s[start]
if c.isdigit():
tmp_int = start + 1
while tmp_int < end:
if s[tmp_int].isdigit():
tmp_int += 1
else:
break
multi = int(s[start:tmp_int])
start = tmp_int + 1
left = 1
right = 0
for i in range(start, end):
char = s[i]
if char == '[':
left += 1
if char == ']':
right += 1
if left == right:
tmp = self.decodeString(s[start:i])
res += multi * tmp
start = i + 1
break
else:
res += s[start]
start += 1
continue
return res