ntree.aiWord Ladder
Word Ladder
hardsolvedattempted
You are given two words, beginWord and endWord, and a dictionary wordList. All words have the same length.
A ladder is a sequence of words that starts with beginWord and ends with endWord, where each word differs from the one before it in exactly one letter, and every word after the first is in wordList. beginWord does not have to be in wordList.
Return the number of words in the shortest ladder, or 0 if there is none.
Example 1:
Input: beginWord = "hit", endWord = "cog",
wordList = ["hot", "dot", "dog", "lot", "log", "cog"]
Output: 5
Explanation: one shortest ladder is "hit" → "hot" → "dot" → "dog" → "cog", five words long.
Example 2:
Input: beginWord = "hit", endWord = "cog",
wordList = ["hot", "dot", "dog", "lot", "log"]
Output: 0
Explanation: "cog" is not in the dictionary, so no ladder can end there.
Constraints:
1 <= length of beginWord <= 10endWordand every word inwordListhave the same length asbeginWord1 <= length of wordList <= 5000- all words are made of lowercase English letters
- the words in
wordListare distinct beginWord != endWord
def ladderLength(beginWord: str, endWord: str, wordList: list[str]) -> int: pass Press Visualise to step through your code.