-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchForWord.java
39 lines (33 loc) · 1.16 KB
/
SearchForWord.java
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
public class SearchForWord {
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == word.charAt(0)) {
if (dfs(board, word, 0, i, j, m, n)) {
return true;
}
}
}
}
return false;
}
private boolean dfs(char[][] board, String word, int index, int i, int j, int m, int n) {
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != word.charAt(index)) {
return false;
}
if (index == word.length() - 1) {
return true;
}
board[i][j] = '#';
if (dfs(board, word, index + 1, i - 1, j, m, n)
|| dfs(board, word, index + 1, i + 1, j, m, n)
|| dfs(board, word, index + 1, i, j - 1, m, n)
|| dfs(board, word, index + 1, i, j + 1, m, n)) {
return true;
}
board[i][j] = word.charAt(index);
return false;
}
}