ntree.ai
ntree.aiWord Ladder II

Word Ladder II

hardsolvedattempted

Given two words beginWord and endWord and a list of words wordList, return every shortest transformation sequence from beginWord to endWord.

A transformation sequence changes one letter at a time, and every word after beginWord must be in wordList. beginWord does not need to be in wordList.

Return each sequence as a list of words, from beginWord to endWord. If there is no sequence, return an empty list. The answer may be returned in any order.

Example 1:

Input:  beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log", "cog"]
Output: [["hit", "hot", "dot", "dog", "cog"], ["hit", "hot", "lot", "log", "cog"]]
Explanation: both sequences take four changes, and no sequence takes fewer.

Example 2:

Input:  beginWord = "hit", endWord = "cog", wordList = ["hot", "dot", "dog", "lot", "log"]
Output: []
Explanation: "cog" is not in wordList, so it cannot be reached.

Constraints:

  • 1 <= length of beginWord <= 5
  • endWord and every word in wordList have the same length as beginWord
  • 1 <= length of wordList <= 500
  • every word holds lowercase English letters only, and the words in wordList are all different
def findLadders(beginWord: str, endWord: str, wordList: list[str]) -> list[list[str]]:
pass
 

Press Visualise to step through your code.