-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecoding-String.py
More file actions
47 lines (41 loc) · 1.02 KB
/
Decoding-String.py
File metadata and controls
47 lines (41 loc) · 1.02 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
"""
s = 3[a]2[bc]
=> aaabcbc
s=3[a2[c]]xy
==> accaccaccxy
"""
def decoding_string(s):
int_stack = list()
char_stack = list()
i = 0
t_str = ''
while i < len(s):
if s[i].isdigit():
if s[i+1].isdigit():
int_stack.append(int(s[i]+s[i+1]))
i += 1
else:
int_stack.append(int(s[i]))
else:
if s[i] == ']':
ctop = char_stack.pop()
while ctop != '[':
t_str = ctop + t_str
ctop = char_stack.pop()
c_int = int_stack.pop()
m_str = ''
for j in range(c_int):
m_str += t_str
char_stack.append(m_str)
t_str = ''
else:
char_stack.append(s[i])
i += 1
while char_stack:
ctop = char_stack.pop()
t_str = ctop + t_str
return t_str
s = '3[a]2[bc]'
print(decoding_string(s))
s='3[a2[c]]xy'
print(decoding_string(s))