Word Ladder II
Problem statement
Given two distinct lowercase words beginWord and endWord, and an array wordList of distinct lowercase words of the same length, return every shortest transformation sequence from beginWord to endWord.
In one transformation, change exactly one character. Every word after beginWord, including endWord, must appear in wordList. If no sequence exists, return an empty array.
Sort the returned sequences lexicographically by their complete word sequence.
Function
findLadders(beginWord: String, endWord: String, wordList: String[]) → String[][]Examples
Example 1
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log","cog"]return = [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]Both sequences use four transformations, and no shorter path reaches cog.
Example 2
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log"]return = []The required end word is absent from wordList, so no valid sequence exists.
Example 3
beginWord = "a"endWord = "c"wordList = ["a","b","c"]return = [["a","c"]]The one-character words differ in exactly one position, so the direct transformation is shortest.
Constraints
1 <= beginWord.length == endWord.length <= 101 <= wordList.length <= 500- All words contain only lowercase English letters and have the same length.
- All words in
wordListare distinct. beginWord != endWord.- The total returned output contains at most
10000words.