-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWordSearch.java
More file actions
56 lines (50 loc) · 1.41 KB
/
WordSearch.java
File metadata and controls
56 lines (50 loc) · 1.41 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
public class WordSearch {
static boolean isValid = false;
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(isMatch(i, j, board, word)) {
return true;
}
}
}
return false;
}
static boolean isMatch(int row, int col, char[][]board, String word) {
if(word.length() == 0) {
return true;
}
if(row < 0 || col < 0 || row == board.length || col == board[0].length || board[row][col] != word.charAt(0)) {
return false;
}
board[row][col] = '#';
int directions[][] = {
{0,1}, // right
{1,0}, // down
{0,-1}, // left
{-1,0} // up
};
for(int direction = 0; direction < directions.length; direction++) {
int rowDirection = directions[direction][0];
int colDirection = directions[direction][1];
isValid = isMatch(row + rowDirection, col + colDirection, board, word.substring(1));
if(isValid) {
break;
}
}
board[row][col] = word.charAt(0);
return isValid;
}
public static void main(String[] args) {
char board[][] = {
{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}
};
//String word = "ABCCED";
//String word = "SEED";
String word = "ABCB";
boolean result = exist(board, word);
System.out.println(result);
}
}