-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
103 lines (96 loc) · 2.65 KB
/
lib.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
/***
Given an m x n grid of characters board and a string word, return true if word
exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where
adjacent cells are horizontally or vertically neighboring. The same letter cell
may not be used more than once.
***/
use std::collections::HashSet;
pub fn exist(board: Vec<Vec<char>>, word: String) -> bool {
if board.len() == 0 {
return false;
}
let rows = board.len();
let cols = board[0].len();
let word: Vec<_> = word.chars().collect();
let mut path = HashSet::<(i32, i32)>::with_capacity(word.len());
for row in 0..rows {
for col in 0..cols {
if search(
&board,
&word,
row as i32,
col as i32,
0,
&mut path,
rows as i32,
cols as i32,
) {
return true;
}
}
}
false
}
fn search(
board: &Vec<Vec<char>>,
word: &Vec<char>,
row: i32,
col: i32,
index: usize,
path: &mut HashSet<(i32, i32)>,
rows: i32,
cols: i32,
) -> bool {
if index == word.len() {
return true;
}
let cell = (row, col);
if row >= rows
|| row < 0
|| col >= cols
|| col < 0
|| path.contains(&cell)
|| board[row as usize][col as usize] != word[index]
{
return false;
}
path.insert(cell.clone());
let found = search(board, word, row + 1, col, index + 1, path, rows, cols)
|| search(board, word, row - 1, col, index + 1, path, rows, cols)
|| search(board, word, row, col + 1, index + 1, path, rows, cols)
|| search(board, word, row, col - 1, index + 1, path, rows, cols);
path.remove(&cell);
found
}
#[cfg(test)]
mod tests {
use super::exist;
#[test]
fn test_exist() {
let board = vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E'],
];
let word = String::from("ABCCED");
let result = exist(board, word);
assert!(result);
let board = vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E'],
];
let word = String::from("SEE");
let result = exist(board, word);
assert!(result);
let board = vec![
vec!['A', 'B', 'C', 'E'],
vec!['S', 'F', 'C', 'S'],
vec!['A', 'D', 'E', 'E'],
];
let word = String::from("ABCB");
let result = exist(board, word);
assert!(!result);
}
}