0%

394. Decode String

https://leetcode.com/problems/decode-string/

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
from collections import deque
class Solution:
def decodeString(self, A: str) -> str:
def f():
p=i
base=1
res=0
while Z and Z[-1].isdigit():
res+=base*int(Z.pop())
base*=10
return res

Z=[]
i=0
while i<len(A):
a=A[i]
if a==']':
tmp=deque()
while Z:
x=Z.pop()
if x=='[':
k=f()
tmp=k*tmp
break
tmp.appendleft(x)
Z.extend(tmp)
i+=1
continue
if a!=']':
Z.append(a)
i+=1
return "".join(Z)