forked from fantastic-Feifei/Leetcode_Solutions_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394_decodeString.py
More file actions
35 lines (23 loc) · 841 Bytes
/
394_decodeString.py
File metadata and controls
35 lines (23 loc) · 841 Bytes
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
class Solution:
def decodeString(self, s: str) -> str:
def dfs(position: int):
res = ""
multi_round = 0
while position < len(s):
if '0' <= s[position] <= '9':
multi_round = multi_round*10 + int(s[position])
elif s[position] == '[':
position, tmp = dfs(position + 1)
res += multi_round * tmp
multi_round = 0
elif s[position] == ']':
return position, res
else:
res += s[position]
position += 1
return res
return dfs(0)
s = "3[a]2[bc]"
ob = Solution()
ans = ob.decodeString(s)
ans