forked from tanglu/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordSearch.java
52 lines (46 loc) · 1.17 KB
/
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
46
47
48
49
50
51
52
public class Solution {
public boolean exist(char[][] board, String word) {
if(word.length()==0) {
return false;
}
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i=0;i<board.length;i++) {
for (int j=0;j<board[i].length;j++) {
visited[i][j] = false;
}
}
for (int i=0;i<board.length;i++) {
for (int j=0;j<board[i].length;j++) {
if(board[i][j]==word.charAt(0)) {
visited[i][j] = true;
if(word.length()==1 || search(board,i,j,word.substring(1),visited)) {
return true;
}
visited[i][j] = false;
}
}
}
return false;
}
boolean search(char[][] board, int i, int j, String word, boolean [][]visited) {
if(word.length()==0) {
return true;
}
int[][] direction = { {-1,0},{1,0},{0,-1},{0,1}};
for (int k=0;k<direction.length;k++) {
int ii = i + direction[k][0];
int jj = j + direction[k][1];
if ( ii>=0&&ii<board.length&&
jj>=0&&jj<board[i].length&&
board[ii][jj]==word.charAt(0)&&
visited[ii][jj]==false) {
visited[ii][jj] = true;
if (word.length()==1 || search(board,ii,jj,word.substring(1),visited)) {
return true;
}
visited[ii][jj] = false;
}
}
return false;
}
}