Python using stack


  • 0
    B
    class Solution(object):
        def decodeString(self, s):
            """
            :type s: str
            :rtype: str
            """
            if len(s) < 1: return ""
            stack = []
            for ch in s:
                if ch !=']':
                    stack.append(ch)
                else:
                    l = []
                    while True:
                        ch1 = stack.pop()
                        if ch1 != '[':
                            l.append(ch1)
                        else:
                            num = ""
                            while stack and stack[-1].isdigit():
                                    num += stack.pop()
                            l *= int(num[::-1])
                            stack += l[::-1]
                            break
            return "".join(stack)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.