-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecodeString.py
More file actions
33 lines (27 loc) · 945 Bytes
/
decodeString.py
File metadata and controls
33 lines (27 loc) · 945 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
"""
Given a string with a certain rule: k[string] should be expanded to string k times. So for example, 3[abc] should be expanded to abcabcabc. Nested expansions can happen, so 2[a2[b]c] should be expanded to abbcabbc.
Your starting point:
def decodeString(s):
# Fill this in.
print decodeString('2[a2[b]c]')
# abbcabbc
"""
def decodeString(s):
result = ""
stack = []
i = 0
while i < len(s):
if s[i] == "]":
temp = ""
while stack and stack[-1] != "[":
temp = stack.pop() + temp
stack.pop() # remote [
count = ""
while stack and stack[-1].isdigit():
count = stack.pop() + count
stack.append(int(count)*temp)
else:
stack.append(s[i])
i += 1
return "".join(stack)
print decodeString('2[a2[b]c]')