A/C Python solution, easy to understand, beat 55.30%


  • 0
    W

    class Solution(object):
    def init(self):

        self.res = ""
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
    
        #print "s = ", s
        numList = ['0','1','2','3','4','5','6','7','8','9']
        #print "numList = ", numList
    
        resQ = ""
    
        numQ = []
        curNum = ""
        stringQ = []
    
        lenS = len(s)
        #print "lenS = ", lenS
    
        curIdx = 0
    
    
        def helper(curStr):
            #print "in helper, curStr = ", curStr
    
            idxFirstRightBracket = curStr.find("]")
            if idxFirstRightBracket == -1:
                return
            #print "idxFirstRightBracket = ", idxFirstRightBracket
    
            #need to find the corresponding [
            idxPairLeftBracket = curStr[0:idxFirstRightBracket].rfind("[")
            #print "idxPairLeftBracket = ", idxPairLeftBracket
    
            numberIdx = idxPairLeftBracket - 1
            while numberIdx >= 0 and curStr[numberIdx] in  numList:
                numberIdx -= 1
            #print "numberIdx = ", numberIdx
    
            #print "number = ", curStr[numberIdx+1: idxPairLeftBracket]
    
            tmpStr = ""
            #print "curStr[numberIdx: idxPairLeftBracket] = ", curStr[idxPairLeftBracket+1: idxFirstRightBracket]
    
            intNum = int(curStr[numberIdx+1: idxPairLeftBracket])
            #print "intNum = ", intNum
            for i in range(intNum):
                tmpStr += curStr[idxPairLeftBracket+1: idxFirstRightBracket]
    
            #print "tmpStr = ", tmpStr
    
            curStr = curStr[0:numberIdx+1] + tmpStr + curStr[idxFirstRightBracket+1:]
            #print "curStr = ", curStr
    
            self.res = curStr
    
            if curStr.find("[") >=0:
                helper(curStr)
    
        self.res = s
        helper(s)
    
        return self.res

Log in to reply
 

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