-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWordSearch.java
More file actions
40 lines (36 loc) · 1.05 KB
/
WordSearch.java
File metadata and controls
40 lines (36 loc) · 1.05 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
public class Solution {
public boolean exist(char[][] board, String word) {
// Start typing your Java solution below
// DO NOT write main() function
int m = board.length;
int n = board[0].length;
if(word.length()>m*n) return false;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++){
if(board[i][j]==word.charAt(0)){
int[][] used = new int[m][n];
used[i][j]=1;
if(findString(board,i,j,used,word.substring(1)))
return true;
}
}
return false;
}
public boolean findString(char[][] board, int i, int j, int[][] used, String word){
if(word.length()==0)
return true;
char temp = word.charAt(0);
for(int p=-1; p<2; p++)
for(int q=-1;q<2;q++){
if((i+p>=0 && i+p<board.length) && (j+q>=0 && j+q<board[0].length) && ((Math.abs(p))^(Math.abs(q)))!=0){
if(board[i+p][j+q] == temp && used[i+p][j+q]==0){
used[i+p][j+q]=1;
if(findString(board, i+p, j+q, used, word.substring(1)))
return true;
used[i+p][j+q]=0;
}
}
}
return false;
}
}