-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeetCodeWordSearchProblem
More file actions
47 lines (46 loc) · 1.49 KB
/
LeetCodeWordSearchProblem
File metadata and controls
47 lines (46 loc) · 1.49 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
class Solution {
char[][] board;
public boolean exist(char[][] board, String word){
this.board = board;
//exist(0,0, word);
for(int i = 0; i<board.length; i++){
for(int j = 0; j<board[i].length; j++){
if(exist(i, j, word)){
return true;
}
}
}
return false;
}
boolean isValidPath = false;
public boolean exist(int row, int col, String word) {
// word found
if(word.length() ==0){
return true;
}
// if word not found and goes out of range
if(row<0 || col<0 || row == board.length || col==board[row].length || board[row][col]!=word.charAt(0)){
return false;
}
// Mark as Visited
board[row][col] = '#'; // Mark as visited.
// Lookup in all 4 Directions
int directions [][] = {
{0,1},// right
{1,0},// down
{-1,0},// up
{0,-1} // left
};
for(int direction =0; direction<directions.length; direction++){
int rowDirection = directions[direction][0]; // get row
int colDirection = directions[direction][1]; // get col
isValidPath = exist(row + rowDirection, col + colDirection, word.substring(1));
if(isValidPath){
break;
}
}
// BackTracking (Undo)
board[row][col] = word.charAt(0);
return isValidPath;
}
}