Python Solution with Recursion


  • 0
    class Solution(object):
        def decodeString(self, s):
            decode = ''
            i = 0
            while i in range(len(s)):
                if s[i].isalpha():
                    decode += s[i]
                    i += 1
                elif s[i].isdigit():
                    multiple = ''
                    while s[i].isdigit():
                        multiple += s[i]
                        i += 1
                    multiple = int(multiple)
                    ret, i_new  = self.decodeString(s[i+1:])
                    decode += ret * multiple
                    i += i_new + 1
                elif s[i] == '[':
                    i += 1
                elif s[i] == ']':
                    i += 1
                    return decode, i
            return decode
    

Log in to reply
 

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