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
@StevenYU nice guess, haha! but i still think something is not proper with this case.
@iwantgoogle Yeah there's something wrong
Here's the transform sequence
hit, hot, dot, dog, cog (5)
But I get expected = 0;
It's been a few months since the last post and I still see this test case failing. As @brichards pointed out, transformation is possible from "hit" -> "cog" with the sequence provided. The word list contains intermediate words allowed during the transform and doesn't have to include the end word. How can we get this fixed?
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.