forked from kishanrajput23/leetcode-solutions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
70 lines (66 loc) · 2.32 KB
/
WordSearch.java
File metadata and controls
70 lines (66 loc) · 2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// https://leetcode.com/problems/word-search/
public class WordSearch {
public static void main(String[] args) {
char[][] board = {
{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}
};
boolean ans = exist(board, "ABCCED");
System.out.println(ans);
}
public static boolean exist(char[][] board, String word) {
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == word.charAt(0)){
char temp = board[i][j];
board[i][j] = '0';
if(isValid(board, i, j, 1, word)){
return true;
}
board[i][j] = temp;
}
}
}
return false;
}
public static boolean isValid(char[][] board, int row, int col, int index, String word){
if(index == word.length()){
return true;
}
char temp;
if(col != 0 && board[row][col - 1] == word.charAt(index)){
temp = board[row][col - 1];
board[row][col - 1] = '0';
if(isValid(board, row, col-1, index+1, word)){
return true;
}
board[row][col-1] = temp;
}
if(col <= board[0].length-2 && board[row][col + 1] == word.charAt(index)){
temp = board[row][col + 1];
board[row][col + 1] = '0';
if(isValid(board, row, col+1, index+1, word)){
return true;
}
board[row][col+1] = temp;
}
if(row != 0 && board[row - 1][col] == word.charAt(index)){
temp = board[row - 1][col];
board[row - 1][col] = '0';
if(isValid(board, row - 1, col, index+1, word)){
return true;
}
board[row - 1][col] = temp;
}
if(row <= board.length - 2 && board[row + 1][col] == word.charAt(index)){
temp = board[row + 1][col];
board[row + 1][col] = '0';
if(isValid(board, row + 1, col, index+1, word)){
return true;
}
board[row + 1][col] = temp;
}
return false;
}
}