-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path027. WordSearch.java
More file actions
64 lines (58 loc) · 2.37 KB
/
027. WordSearch.java
File metadata and controls
64 lines (58 loc) · 2.37 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
55
56
57
58
59
60
61
62
63
64
class Solution {
static public boolean isWordExist(char[][] mat, String word) {
char first = word.charAt(0);
int n = mat.length;
int m = mat[0].length;
boolean[][] visited = new boolean[n][m];
int row = 0;
int col = 0;
boolean found = false;
for(int i = 0; i<mat.length; i++) {
for (int j = 0; j<mat[i].length; j++) {
if(mat[i][j] == first) {
row = i;
col = j;
visited[i][j] = true;
found= checkNearby(mat,row,col,word,1,"huu",visited);
if(found) {
return found;
}
}
visited[i][j] = false;
}
}
return found;
}
public static boolean checkNearby(char[][] mat, int i,int j, String word, int wordIndex, String notCheck, boolean[][] visited) {
if(wordIndex>=word.length()) {
return true;
}
boolean right = false;
boolean left = false;
boolean up = false;
boolean down = false;
if(!Objects.equals(notCheck,"right") && j+1<mat[i].length && mat[i][j+1]==word.charAt(wordIndex) && !visited[i][j+1]) {
visited[i][j+1] = true;
right = checkNearby(mat,i,j+1,word,wordIndex+1,"left",visited);
if(!right)
visited[i][j+1] = false;
} if(!Objects.equals(notCheck,"left") && j-1>=0 && mat[i][j-1]== word.charAt(wordIndex) && !visited[i][j-1]) {
visited[i][j-1] = true;
left = checkNearby(mat,i,j-1,word,wordIndex+1,"right",visited);
if(!left)
visited[i][j-1] = false;
} if(!Objects.equals(notCheck,"down") && i+1<mat.length && mat[i+1][j] == word.charAt(wordIndex) && !visited[i+1][j]) {
visited[i+1][j] = true;
up = checkNearby(mat,i+1,j,word,wordIndex+1,"up",visited);
if(!up)
visited[i+1][j] = false;
}
if(!Objects.equals(notCheck,"up") && i-1>=0 && mat[i-1][j] == word.charAt(wordIndex) && !visited[i-1][j]) {
visited[i-1][j] = true;
down = checkNearby(mat,i-1,j,word,wordIndex+1,"down",visited);
if(!down)
visited[i-1][j] = false;
}
return right || left || up || down;
}
}