-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00079-word_search.java
49 lines (37 loc) · 1.26 KB
/
00079-word_search.java
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
// 79: Word Search
// https://leetcode.com/problems/word-search
class Solution {
public boolean backtrack(char[][] board, int i, int j, String word) {
if (word.length() == 0) return true;
if (i<0 || i>=board.length || j<0 || j>=board[0].length || board[i][j]!=word.charAt(0)) return false;
char c = board[i][j];
board[i][j] = '*';
String s = word.substring(1);
boolean result = backtrack(board, i-1, j, s)
|| backtrack(board, i+1, j, s)
|| backtrack(board, i, j-1, s)
|| backtrack(board, i, j+1, s);
board[i][j] = c;
return result;
}
// SOLUTION
public boolean exist(char[][] board, String word) {
for (int i=0; i<board.length; i++)
for (int j=0; j<board[0].length; j++)
if (backtrack(board, i, j, word))
return true;
return false;
}
public static void main(String[] args) {
Solution s = new Solution();
// INPUT :
char[][] board = {
{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}
};
String word = "ABCCED";
// OUTPUT :
System.out.println(s.exist(board, word));
}
}