127. Word Ladder Hard
1/**
2 * [127] Word Ladder
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 the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
11 *
12 * Example 1:
13 *
14 * Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
15 * Output: 5
16 * Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
17 *
18 * Example 2:
19 *
20 * Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
21 * Output: 0
22 * Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= beginWord.length <= 10
28 * endWord.length == beginWord.length
29 * 1 <= wordList.length <= 5000
30 * wordList[i].length == beginWord.length
31 * beginWord, endWord, and wordList[i] consist of lowercase English letters.
32 * beginWord != endWord
33 * All the words in wordList are unique.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/word-ladder/
39// discuss: https://leetcode.com/problems/word-ladder/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn ladder_length(begin_word: String, end_word: String, word_list: Vec<String>) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_127() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.