Skip to content

Commit 2c414e0

Browse files
committed
decode_string
1 parent 645cff3 commit 2c414e0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Task/Decode_string.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def decode_string(s):
2+
stack = []
3+
curr_num = 0
4+
curr_str = ""
5+
i = 0
6+
while i < len(s):
7+
if s[i].isdigit():
8+
num = 0
9+
# Handle numbers with more than one digit
10+
while i < len(s) and s[i].isdigit():
11+
num = num * 10 + int(s[i])
12+
i += 1
13+
curr_num = num
14+
elif s[i] == "{" or s[i] == "(":
15+
# Push current status into stack and reset
16+
stack.append((curr_str, curr_num))
17+
curr_str = ""
18+
curr_num = 0
19+
i += 1
20+
elif s[i] == "}" or s[i] == ")":
21+
# Pop from stack and decode
22+
last_str, num = stack.pop()
23+
curr_str = last_str + curr_str * num
24+
i += 1
25+
else:
26+
curr_str += s[i]
27+
i += 1
28+
return curr_str
29+
30+
31+
specialString = input()
32+
print(decode_string(specialString))

0 commit comments

Comments
 (0)