-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_search_ii.rs
139 lines (121 loc) · 3.14 KB
/
word_search_ii.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#![allow(dead_code)]
use std::collections::{HashMap, HashSet};
pub struct TrieNode {
pub children: HashMap<char, TrieNode>,
pub is_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_word: false,
}
}
fn insert(&mut self, word: &str) {
let mut node = self;
for c in word.chars() {
node = node.children.entry(c).or_insert(TrieNode::new());
}
node.is_word = true;
}
}
fn find_words(board: Vec<Vec<char>>, words: Vec<&str>) -> Vec<String> {
let mut root = TrieNode::new();
for word in words {
root.insert(word);
}
let rows = board.len();
let cols = board[0].len();
let mut path = HashSet::new();
let mut res = HashSet::new();
fn dfs(
r: usize,
c: usize,
word: &mut String,
node: &mut TrieNode,
path: &mut HashSet<(usize, usize)>,
res: &mut HashSet<String>,
board: &[Vec<char>],
rows: usize,
cols: usize,
) {
if r >= rows
|| c >= cols
|| !node.children.contains_key(&board[r][c])
|| path.contains(&(r, c))
{
return;
}
path.insert((r, c));
word.push(board[r][c]);
if node.is_word {
res.insert(word.clone());
}
if let Some(child_node) = node.children.get_mut(&board[r][c]) {
if child_node.is_word {
res.insert(word.clone());
}
dfs(r + 1, c, word, child_node, path, res, board, rows, cols);
dfs(
r.saturating_sub(1),
c,
word,
child_node,
path,
res,
board,
rows,
cols,
);
dfs(r, c + 1, word, child_node, path, res, board, rows, cols);
dfs(
r,
c.saturating_sub(1),
word,
child_node,
path,
res,
board,
rows,
cols,
);
}
path.remove(&(r, c));
word.pop();
}
for r in 0..rows {
for c in 0..cols {
let mut word = String::new();
dfs(
r, c, &mut word, &mut root, &mut path, &mut res, &board, rows, cols,
);
}
}
res.into_iter().collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn compare_two_vec_of_string(v1: Vec<String>, v2: Vec<&str>) {
let mut v2 = v2.to_vec();
v2.sort();
let mut v1 = v1;
v1.sort();
assert_eq!(v1, v2);
}
#[test]
fn test_find_words() {
compare_two_vec_of_string(
find_words(
vec![
vec!['o', 'a', 'a', 'n'],
vec!['e', 't', 'a', 'e'],
vec!['i', 'h', 'k', 'r'],
vec!['i', 'f', 'l', 'v'],
],
vec!["oath", "pea", "eat", "rain"],
),
vec!["eat", "oath"],
);
}
}