-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
48 lines (37 loc) · 1.34 KB
/
Copy pathdecoder.py
File metadata and controls
48 lines (37 loc) · 1.34 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
class Solution:
def decodeString(self, decode_me: str) -> str:
def decode(left, right) -> str:
x = 0
l = 0
bal = 0
s = ''
ret = ''
for i in range(left, right+1):
c = decode_me[i]
if c == '[':
if bal == 0:
l = i
bal += 1
continue
if c == ']':
if bal == 1:
ret += x * decode(l+1, i-1)
l = -1
x = 0
bal -= 1
continue
if c.isdigit():
ret += s
if x == 0:
s = ''
if bal == 0:
x *= 10
x += ord(c) - ord('0')
else:
if bal == 0:
s += c
return ret + s
return decode(0, len(decode_me)-1)
print(Solution().decodeString(decode_me="3[a]2[bc]"))
print(Solution().decodeString(decode_me="3[a2[c]]"))
print(Solution().decodeString(decode_me="2[abc]3[cd]ef"))