-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm394.py
33 lines (26 loc) · 1.02 KB
/
m394.py
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
class Solution:
def decodeString(self, s: str) -> str:
output = []
currentIndx = 0
while currentIndx < len(s) :
if s[currentIndx].isnumeric() :
numStart = currentIndx
while s[currentIndx].isnumeric() :
currentIndx += 1
bracketStart = currentIndx
num = int(s[numStart:bracketStart])
numOpeners = 1
while numOpeners > 0 :
currentIndx += 1
if s[currentIndx] == '[' :
numOpeners += 1
elif s[currentIndx] == ']' :
numOpeners -= 1
ending = currentIndx
middle = self.decodeString(s[bracketStart + 1: ending])
output.extend(num * [middle])
currentIndx += 1
else :
output.append(s[currentIndx])
currentIndx += 1
return ''.join(output)