Word Ladder
A ladder from beginWord to endWord is a sequence of words where each word differs from the one before it in exactly one letter, every word after the first comes from the list words, and the last word is endWord.
Return the number of words in the shortest ladder, counting beginWord and endWord, or 0 if there is no ladder. beginWord doesn't have to be in words; endWord has to be.
Constraints
- 1 ≤ length of each word ≤ 8, and every word has the same length
- 1 ≤ length of
words≤ 3,000 - All words are lower-case English letters; the words in
wordsare different, andbeginWord≠endWord.
Example 1
Input
begin_word = "hit"
end_word = "cog"
words = ["hot","dot","dog","lot","log","cog"]
Output
5
Why:
hit, hot, dot, dog, cog.
Example 2
Input
begin_word = "hit"
end_word = "cog"
words = ["hot","dot","dog","lot","log"]
Output
0
Why:
cog isn't in the list, so no ladder can end there.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –