A/C Python solution, use {}, easy to understand, beat 43%


  • 0
    W

    class Solution(object):
    def groupStrings(self, inputList):
    #print "inputList = ", inputList

        numList = len(inputList)
        #print "numList = ", numList
    
        result = {}
        for string in inputList:
            #print "string = ", string
            lenStr = len(string)
    
            distance = ""
            if lenStr < 1:
                distance = "special"
            elif lenStr == 1:
                distance = "single"
            else:
                for i in range(1, lenStr):
                    #print "c = ", string[i]
                    tmpDis = ord(string[i]) - ord(string[i-1])
                    if tmpDis < 0:
                        tmpDis += 26
                    distance += str(tmpDis)
                    distance += "-"
    
    
            #print "distance = ", distance
            if distance not in result:
                result[distance] = [string]
            else:
                result[distance].append(string)
    
    
        #print "result = ", result
        #print "result.values() = ", result.values()
        return result.values()

Log in to reply
 

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