forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
40 lines (37 loc) · 1.32 KB
/
WordSearch.java
File metadata and controls
40 lines (37 loc) · 1.32 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
package LeetCode;
public class WordSearch {
public boolean exist(char[][] board, String word) {
int r=board.length;
int w=board[0].length;
boolean visited[][]=new boolean[r][w];
for (int i=0;i<r;i++){
for (int j=0;j<w;j++){
if(explore(board,visited,word,i,j,0)){
return true;
}
}
}
return false;
}
private boolean explore(char[][] board, boolean[][] visited, String word, int i, int j, int start) {
if(start>=word.length()){
return true;
}
if(i<0||i>=board.length||j<0||j>=board[0].length||visited[i][j]||word.charAt(start)!=board[i][j]){
return false;
}
visited[i][j]=true;
boolean result=explore(board,visited,word,i+1,j,start+1)||
explore(board,visited,word,i-1,j,start+1)||
explore(board,visited,word,i,j-1,start+1)||
explore(board,visited,word,i,j+1,start+1);
visited[i][j]=false;
return result;
}
public static void main(String[] args){
char[][] input={{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
String word="SEE";
WordSearch wordSearch=new WordSearch();
System.out.println(wordSearch.exist(input,word));
}
}