We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 645cff3 commit 2c414e0Copy full SHA for 2c414e0
1 file changed
Task/Decode_string.py
@@ -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
18
19
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
25
+ else:
26
+ curr_str += s[i]
27
28
+ return curr_str
29
+
30
31
+specialString = input()
32
+print(decode_string(specialString))
0 commit comments