-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_ladder.rs
108 lines (93 loc) · 3.15 KB
/
word_ladder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::collections::HashSet;
use std::collections::VecDeque;
/// A transformation sequence from word `beginWord` to word `endWord` using a dictionary `wordList`
/// is a sequence of words `beginWord -> s1 -> s2 -> ... -> sk` such that:
///
/// * Every adjacent pair of words differs by a single letter.
///
/// * Every `si` for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in
/// `wordList`.
///
/// * `sk == endWord`
///
/// * 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.
struct Solution;
impl Solution {
fn distance(word1: &String, word2: &String) -> usize {
let mut result = 0;
let mut word1_chars = word1.chars();
let mut word2_chars = word2.chars();
loop {
match (word1_chars.next(), word2_chars.next()) {
(Some(a), Some(b)) => {
if a != b {
result += 1;
}
}
_ => {
break;
}
}
}
result
}
// TODO: Preprocess word_list to make the solution faster
pub fn ladder_length(begin_word: String, end_word: String, word_list: Vec<String>) -> i32 {
let mut finished = false;
let mut result = 0;
let mut seen = HashSet::new();
let mut queue = VecDeque::new();
seen.insert(begin_word.clone());
queue.push_back(begin_word);
while !queue.is_empty() {
let n = queue.len();
for _ in 0..n {
let word = queue.pop_front().unwrap();
if word == end_word {
finished = true;
break;
} else {
for possible in &word_list {
if !seen.contains(possible) && Self::distance(&word, possible) == 1 {
seen.insert(possible.clone());
queue.push_back(possible.clone());
}
}
}
}
result += 1;
if finished {
break;
}
}
if finished { result } else { 0 }
}
}
#[cfg(test)]
mod tests {
use super::Solution;
fn vec_strings(items: Vec<&str>) -> Vec<String> {
items
.into_iter()
.map(String::from)
.collect()
}
#[test]
fn example_1() {
let begin_word = String::from("hit");
let end_word = String::from("cog");
let word_list = vec_strings(vec!["hot","dot","dog","lot","log","cog"]);
let result = Solution::ladder_length(begin_word, end_word, word_list);
assert_eq!(result, 5);
}
#[test]
fn example_2() {
let begin_word = String::from("hit");
let end_word = String::from("cog");
let word_list = vec_strings(vec!["hot","dot","dog","lot","log"]);
let result = Solution::ladder_length(begin_word, end_word, word_list);
assert_eq!(result, 0);
}
}