-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_string.py
More file actions
32 lines (30 loc) · 893 Bytes
/
Decode_string.py
File metadata and controls
32 lines (30 loc) · 893 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
def decode_string(s):
stack = []
curr_num = 0
curr_str = ""
i = 0
while i < len(s):
if s[i].isdigit():
num = 0
# Handle numbers with more than one digit
while i < len(s) and s[i].isdigit():
num = num * 10 + int(s[i])
i += 1
curr_num = num
elif s[i] == "{" or s[i] == "(":
# Push current status into stack and reset
stack.append((curr_str, curr_num))
curr_str = ""
curr_num = 0
i += 1
elif s[i] == "}" or s[i] == ")":
# Pop from stack and decode
last_str, num = stack.pop()
curr_str = last_str + curr_str * num
i += 1
else:
curr_str += s[i]
i += 1
return curr_str
specialString = input()
print(decode_string(specialString))