-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeString.py
More file actions
41 lines (35 loc) · 1.11 KB
/
decodeString.py
File metadata and controls
41 lines (35 loc) · 1.11 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
# s = "30[a]2[bc]", return "aaabcbc".
# s = "3[a2[c]]", return "accaccacc".
# s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
s = "10[abc]3[cd]ef"
import pdb
def decodeString(s):
stack = []
l = len(s)
for i in range(l):
if s[i] != ']':
stack.append(s[i])
else:
st=''
while(1):
xx = stack.pop()
if xx != '[':
st = st+xx
else:
num=''
while(1):
try:
if stack:
if 48 <= ord(stack[-1]) <= 57:
num = num + stack.pop()
else:
break
else: break
except TypeError:
break
temp = st*int(num[::-1])
temp=temp[::-1]
stack.append(temp)
break
return("".join(stack))
print decodeString(s)