Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def decodeString(self, s: str) -> str:
- st=[]
- for i in s:
- #if not '[' just add
- if i!=']':
- st.append(i)
- else:
- # Sinec we got ] we have to pop till we make a block enclosing [ and store it in subs
- subs=''
- while st[-1]!='[':
- subs=st.pop()+subs
- st.pop()
- #now check any number preceding this block. Note this number might have more than one digit. So check
- #till its preceding character is no more a digit
- #Then multiply this number with the block and add to stack again.
- num=''
- while st and st[-1].isdigit():
- num=st.pop()+num
- st.append(subs*int(num))
- #Now currently stack holds all independent blocks and just return them by joining
- return ''.join(st)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement