def dfs(i, s):
n = len(s)
res = []
while i < n and s[i] != ']':
if s[i].isdigit():
j = i
while s[j].isdigit():
j += 1
k = int(s[i:j])
i, l = self.dfs(j + 1, s)
res += l * k
else:
res.append(s[i])
i += 1
return i, res
class Solution:
def decodeString(self, s: str) -> str:
res = []
n = len(s)
i = 0
while i < n:
i, l = dfs(i, s)
res += l
return ''.join(res)