forked from Azureki/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc394. Decode String.py
42 lines (35 loc) · 944 Bytes
/
lc394. 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
class Solution:
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
stack = []
num = ''
i = 0
while i < len(s):
if s[i].isnumeric():
while s[i].isnumeric():
num += s[i]
i += 1
stack.append(num)
num = ''
elif s[i] != ']':
stack.append(s[i])
i += 1
else:
tem = ''
ch = stack.pop()
while ch.isalpha():
tem = ch + tem
ch = stack.pop()
if stack[-1].isnumeric():
n = int(stack.pop())
else:
n = 1
stack.append(tem * n)
i += 1
return ''.join(stack)
sol = Solution()
s = "100[leetcode]"
print(sol.decodeString(s))