An easy-to-understand solution based on recursion.


  • 0
    I

    An easy-to-understand solution based on recursion.

    class Solution(object):
        def decodeString(self, s):
            return self.helper(s,0)
        def helper(self, s, start):
            rtn, nums = '', ''
            while start < len(s):
                if s[start].isdigit() == True:
                    nums += s[start]
                elif s[start] == '[':
                    a, start = self.helper(s, start + 1)
                    rtn += int(nums) * a
                    nums = ''
                elif s[start] == ']':
                    return rtn, start
                else:
                    rtn += s[start]
                start += 1
            return rtn
    

Log in to reply
 

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