forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_79.java
43 lines (37 loc) · 1.38 KB
/
_79.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
package com.fishercoder.solutions;
public class _79 {
public static class Solution1 {
//credit: https://discuss.leetcode.com/topic/21142/my-java-solution
boolean[][] visited;
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (word.charAt(0) == board[i][j] && search(board, word, i, j, 0)) {
return true;
}
}
}
return false;
}
boolean search(char[][] board, String word, int i, int j, int pos) {
if (pos == word.length()) {
return true;
}
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || word.charAt(pos) != board[i][j] || visited[i][j]) {
return false;
}
visited[i][j] = true;
if (search(board, word, i + 1, j, pos + 1)
|| search(board, word, i - 1, j, pos + 1)
|| search(board, word, i, j + 1, pos + 1)
|| search(board, word, i, j - 1, pos + 1)) {
return true;
}
visited[i][j] = false;
return false;
}
}
}