Word Ladder
Written by a community member · · 1 attempt · 0% pass
Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
A transformation changes exactly one letter at a time. Every intermediate word, including endWord, must be in wordList; beginWord does not need to be in the dictionary. The sequence length counts both the starting and ending words. All words contain lowercase English letters and have the same length.
Example 1: beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log", "cog"] returns 5, for example hit -> hot -> dot -> dog -> cog.
Example 2: beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log"] returns 0 because endWord is absent from the dictionary.
Constraints: 1 <= len(beginWord) <= 10; beginWord and endWord have equal length; 1 <= len(wordList) <= 5000; all dictionary words have the same length as beginWord; dictionary words are unique.