Modify the following piece of word ladder code


  • 0
    N

    Modify the following code:

    def GenerateGraph(data):
        d = {}
        g = Graph()
        for word in data:
            for i in range(len(word)):
                bucket = word[:i] + '_' + word[i+1:]
                if bucket in d:
                    d[bucket].append(word)
                else:
                    d[bucket] = [word]
    
        for v in d.keys():
            for word1 in d[v]:
                for word2 in d[v]:
                    if word1 != word2:
                         g.addEdge(word1,word2)
        return g
    

    The objective is to find all combination of words by changing one letter at a time and adding to the graph if word exists in the dictionary.

    The question is to rewrite the code in different logic.


Log in to reply
 

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