-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution12.java
More file actions
54 lines (46 loc) · 1.83 KB
/
Solution12.java
File metadata and controls
54 lines (46 loc) · 1.83 KB
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
class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || word == null || board.length == 0 || word.length() == 0) {
return false;
}
int rowLen = board.length;
int colLen = board[0].length;
boolean[][] visited = new boolean[rowLen][colLen];
for (int row = 0; row < rowLen; row++) {
for (int col = 0; col < colLen; col++) {
visited[row][col] = false;
}
}
for (int row = 0; row < rowLen; row++) {
for (int col = 0; col < colLen; col++) {
if (existCore(board, row, col, visited, word, 0)) {
return true;
}
}
}
return false;
}
private boolean existCore(char[][] board, int rowIndex, int colIndex, boolean[][] visited, String word,
int strIndex) {
if (strIndex >= word.length()) {
return true;
}
if (rowIndex < 0 || colIndex < 0 || rowIndex >= board.length || colIndex >= board[0].length) {
return false;
}
if (visited[rowIndex][colIndex] == false && board[rowIndex][colIndex] == word.charAt(strIndex)) {
visited[rowIndex][colIndex] = true;
boolean result = existCore(board, rowIndex + 1, colIndex, visited, word, strIndex + 1)
|| existCore(board, rowIndex, colIndex + 1, visited, word, strIndex + 1)
|| existCore(board, rowIndex - 1, colIndex, visited, word, strIndex + 1)
|| existCore(board, rowIndex, colIndex - 1, visited, word, strIndex + 1);
if (result) {
return true;
} else {
visited[rowIndex][colIndex] = false;
return false;
}
}
return false;
}
}