Decode String (Leetcode 394)

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

class Solution:
    def decodeString(self, s: str) -> str:
        
        stack=[]
        for i in s:
            if(i!="]"):
                stack.append(i)
            else:
                tempString=""
                while(len(stack)>0 and stack[-1]!="["):
                    tempString=stack.pop()+tempString
                    
                stack.pop()
                
                tempNum=""
                while(len(stack)>0 and stack[-1].isdigit()):
                    tempNum=stack.pop()+tempNum
                tempNum=int(tempNum)
                
                stack.append(tempNum*tempString)
        ans=""
        for i in stack:
            ans=ans+i
        return ans

Last updated