I think there is a temporary glitch in the system. Anybody from the admin team could please look at the issue. Here is the screen shot of showing the wrong answer of the given example:
2::Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
end:"cog" is not find in wordlist
Oh yeah didn't notice that. @yubb is right.
@towhid same problem happens to me. The test case might mean to check if we can give quick answer if endWord is not in the dictionary.
Add this code at front to pass that one:
if endWord not in wordList: return 0
Here is my sample solution:
def ladderLength(self, beginWord, endWord, wordList): if endWord not in wordList: return 0 begin_set, end_set = set([beginWord]), set([endWord]) wordSet = set(wordList) length = 1 alphabets = list(map(chr, range(ord('a'), ord('z')+1))) while begin_set: # find all possible one-change words in the wordList begin_set = wordSet & set(word[:idx] + char + word[idx+1:] for word in begin_set for idx in range(len(beginWord)) for char in alphabets) if begin_set & end_set: # if there are common word in next_level and end_set return length + 1 length += 1 # always proceed on the smaller set if len(begin_set) > len(end_set): begin_set, end_set = end_set, begin_set # remove from wordList all the visted words in next_level, so as to avoid revisiting wordSet -= begin_set return 0
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.