-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWordSearch.java
More file actions
59 lines (54 loc) · 1.88 KB
/
WordSearch.java
File metadata and controls
59 lines (54 loc) · 1.88 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
package leetcode;
/**
* @author eko
* @date 2018/10/25 9:27 AM
*
* Given a 2D board and a word, find if the word exists in the grid.
*
* The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
*
* Example:
*
* board =
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
*
* Given word = "ABCCED", return true.
* Given word = "SEE", return true.
* Given word = "ABCB", return false.
*/
public class WordSearch {
public static void main(String[] args) {
char[][] board =
{
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
String word = "ABCCED";
boolean res = new WordSearch().exist(board, word);
System.out.println(res);
}
public boolean exist(char[][] board, String word) {
char[] chars = word.toCharArray();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (check(i, j, chars, 0, board)) return true;
}
}
return false;
}
public boolean check(int row, int col, char[] chars, int index, char[][] board) {
if (row >= board.length || col >= board[0].length || row < 0 || col < 0) return false;
if (board[row][col] != chars[index]) return false;
if (index == chars.length - 1) return true;
char c = board[row][col];
board[row][col] = '0';
boolean flag = check(row + 1, col, chars, index + 1, board) || check(row - 1, col, chars, index + 1, board) || check(row, col - 1, chars, index + 1, board) || check(row, col + 1, chars, index + 1, board);
board[row][col] = c;
return flag;
}
}