-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path079. Word Search.java
56 lines (52 loc) · 1.96 KB
/
079. Word Search.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
46
47
48
49
50
51
52
53
54
55
56
// https://leetcode.com/problems/word-search
public class Solution {
public boolean helper(char[][]board, String word, boolean[][] visited, int r, int c) {
if (word.length() == 0) {
return true;
}
else {
char nChar = word.charAt(0);
visited[r][c] = true;
if (r - 1 >= 0
&& !visited[r-1][c]
&& board[r-1][c] == nChar
&& helper(board, word.substring(1, word.length()), visited, r-1, c)) {
return true;
}
if (r + 1 < board.length
&& !visited[r+1][c]
&& board[r+1][c] == nChar
&& helper(board, word.substring(1, word.length()), visited, r+1, c)) {
return true;
}
if (c - 1 >= 0
&& !visited[r][c-1]
&& board[r][c-1] == nChar
&& helper(board, word.substring(1, word.length()), visited, r, c-1)) {
return true;
}
if (c + 1 < board[r].length
&& !visited[r][c+1]
&& board[r][c+1] == nChar
&& helper(board, word.substring(1, word.length()), visited, r, c+1)) {
return true;
}
visited[r][c] = false;
return false;
}
}
public boolean exist(char[][] board, String word) {
boolean[][] visited = new boolean[board.length][board[0].length];
if (board.length * board[0].length >= word.length()) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] == word.charAt(0)) {
if(helper(board, word.substring(1, word.length()), visited, r, c))
return true;
}
}
}
}
return false;
}
}