28/37 pass, can not see input for failure...thought my code is logically right


  • 0
    C

    I only pass 28 out of 37.. used BFS here...
    And I cannot see the input of failures..
    0_1479121991212_Screen Shot 2016-11-14 at 3.12.43 AM.png
    my code:

    class Solution(object):
        def ladderLength(self, beginWord, endWord, wordList):
            """
            :type beginWord: str
            :type endWord: str
            :type wordList: Set[str]
            :rtype: int
            """
            current_len = 1
            queue = [(beginWord, current_len)]
            wordList.add(endWord)
            while queue:
                current_word_tuple = queue.pop()
                if current_word_tuple[0] == endWord:
                    return current_word_tuple[1]
                for i in range(len(current_word_tuple[0])):
                    prefix = current_word_tuple[0][:i]
                    postfix = current_word_tuple[0][i + 1:]
                    for insertion in "abcdefghijklmnopqrstuvwxyz":
                        if insertion != current_word_tuple[0][i]:
                            new_word = prefix + insertion + postfix
                            if new_word in wordList:
                                new_len = current_word_tuple[1] + 1
                                queue.append((new_word, new_len))
                                wordList.discard(new_word)
            return 0
    

Log in to reply
 

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