Skip to content

Commit

Permalink
AC
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Nov 6, 2018
1 parent eba2800 commit 83a7b69
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lc394. Decode String.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ def decodeString(self, s):
:rtype: str
"""
stack = []
for c in s:
num = ''
i = 0
while i < len(s):

if c != ']':
stack.append(c)
if s[i].isnumeric():
while s[i].isnumeric():
num += s[i]
i += 1

stack.append(num)
num = ''

elif s[i] != ']':
stack.append(s[i])
i += 1
else:
tem = ''
ch = stack.pop()
Expand All @@ -17,14 +28,15 @@ def decodeString(self, s):
tem = ch + tem
ch = stack.pop()
if stack[-1].isnumeric():
num = int(stack.pop())
n = int(stack.pop())
else:
num = 1
stack.append(tem * num)
n = 1
stack.append(tem * n)
i += 1

return ''.join(stack)


sol = Solution()
s = "3[a2[c]]"
s = "100[leetcode]"
print(sol.decodeString(s))

0 comments on commit 83a7b69

Please sign in to comment.