-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32. WordSearch.java
45 lines (34 loc) · 1.33 KB
/
32. WordSearch.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
40
41
42
43
44
45
class Solution {
public static boolean dfs(int i, int j, char[][] board, int rows, int cols, String word, int idx) {
if (idx == word.length()) return true;
boolean outside = i < 0 || i >= rows || j < 0 || j >= cols;
if (outside || word.charAt(idx) != board[i][j]) return false;
char currChar = board[i][j];
board[i][j] = '*';
boolean wordFound = false;
if (word.charAt(idx) == currChar) {
int[][] directions = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
for (int[] dir: directions) {
int ii = i + dir[0];
int jj = j + dir[1];
if (dfs(ii, jj, board, rows, cols, word, idx + 1)) {
wordFound = true;
break;
}
}
}
board[i][j] = currChar;
return wordFound;
}
static public boolean isWordExist(char[][] mat, String word) {
int rows = mat.length;
int cols = mat[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (word.charAt(0) == mat[i][j] && dfs(i, j, mat, rows, cols, word, 0))
return true;
}
}
return false;
}
}