Still got TLE with my python solution. Is there anyone can help me?


  • 0
    R
    def ladderLength(self, start, end, dict):
        g = self.calPath(start, end, dict)
        return g
    
    def cmpNode(self, string1, string2):
        bit = 0
        for i in xrange(len(string1)):
            if string1[i] != string2[i]:
                bit += 1
        return bit
        
    def calPath(self, start, end, dict):
        q = []
        dis = {}
        q.append(start)
        dis[start] = 1
        find = False
        unvisited = copy.copy(dict)
    
        while q != []:
            word = q.pop(0)
            if word == end:
                find = True
                break
            for newword in unvisited:
                if self.cmpNode(word, newword) == 1 and newword not in dis:
                    dis[newword] = dis[word] + 1
                    q.append(newword)
            unvisited.remove(word)
            
        if find:
            return dis[end]
        else:
            return 0

Log in to reply
 

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