Can anybody help me out? Can't find where the problem is


  • 0
    J
    class Solution(object):
        def neigh(self, wordList, target):
            neighbors = set()
            for i in range(len(target)):
                Target = list(target)
                for j in range(26):
                    Target[i] = chr(j+ord('a'))
                    change = ''.join(Target)
                    if change in wordList:
                        neighbors.add(change)
            return neighbors
        
        def ladderLength(self, beginWord, endWord, wordList):
            """
            :type beginWord: str
            :type endWord: str
            :type wordList: Set[str]
            :rtype: int
            """
            if wordList is None or len(wordList) == 0:
                return 0
            length = 1    
            wordList.add(endWord)
            queue = []
            queue.append(beginWord)
            visited = set()
            visited.add(beginWord)
            while queue:
                length = length + 1
                n = len(queue)
                for i in range(n):
                    node = queue.pop()
                    for neighbor in self.neigh(wordList, node):
                        if neighbor == endWord:
                            return length
                        if neighbor in visited:
                            continue
                        visited.add(neighbor)
                        queue.append(neighbor)
            return 0

Log in to reply
 

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