126. Word Ladder II Hard

@problem@discussion
#Hash Table#String#Backtracking#Breadth-First Search



1/**
2 * [126] Word Ladder II
3 *
4 * A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
5 * 
6 * 	Every adjacent pair of words differs by a single letter.
7 * 	Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
8 * 	sk == endWord
9 * 
10 * Given two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk].
11 *  
12 * Example 1:
13 * 
14 * Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
15 * Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
16 * Explanation: There are 2 shortest transformation sequences:
17 * "hit" -> "hot" -> "dot" -> "dog" -> "cog"
18 * "hit" -> "hot" -> "lot" -> "log" -> "cog"
19 * 
20 * Example 2:
21 * 
22 * Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
23 * Output: []
24 * Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	1 <= beginWord.length <= 5
30 * 	endWord.length == beginWord.length
31 * 	1 <= wordList.length <= 500
32 * 	wordList[i].length == beginWord.length
33 * 	beginWord, endWord, and wordList[i] consist of lowercase English letters.
34 * 	beginWord != endWord
35 * 	All the words in wordList are unique.
36 * 	The sum of all shortest transformation sequences does not exceed 10^5.
37 * 
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/word-ladder-ii/
42// discuss: https://leetcode.com/problems/word-ladder-ii/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47    pub fn find_ladders(begin_word: String, end_word: String, word_list: Vec<String>) -> Vec<Vec<String>> {
48        vec![]
49    }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_126() {
60    }
61}
62


Back
© 2025 bowen.ge All Rights Reserved.