forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_79.java
133 lines (117 loc) · 4.76 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
}
}
// O(1) space solution
public static class Solution2 {
public boolean exist(char[][] board, String word) {
// do DFS traversal
int row = board.length;
int col = board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == word.charAt(0) && search(board, i, j, word, 0) == true) {
return true;
}
}
}
return false;
}
private boolean search(char[][] board, int i, int j, String word, int index) {
if (index == word.length() - 1) {
return true;
}
// store the visited char in a temp variable
char temp = board[i][j];
board[i][j] = ' ';
if (i > 0 && board[i - 1][j] == word.charAt(index + 1) && search(board, i - 1, j, word, index + 1) == true) {
return true;
}
if (i < board.length - 1 && board[i + 1][j] == word.charAt(index + 1) && search(board, i + 1, j, word, index + 1) == true) {
return true;
}
if (j > 0 && board[i][j - 1] == word.charAt(index + 1) && search(board, i, j - 1, word, index + 1) == true) {
return true;
}
if (j < board[0].length - 1 && board[i][j + 1] == word.charAt(index + 1) && search(board, i, j + 1, word, index + 1) == true) {
return true;
}
board[i][j] = temp;
return false;
}
}
public static class Solution3 {
/**
* I came up with below solution completely independently on 10/7/2021, although space complexity is O(m*n) instead of constant.
*/
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == word.charAt(0)) {
visited[i][j] = true;
if (existByDfs(board, i, j, word.substring(1), visited, m, n)) {
return true;
}
//backtracking
visited[i][j] = false;
}
}
}
return false;
}
int[] directions = new int[]{0, 1, 0, -1, 0};
private boolean existByDfs(char[][] board, int startI, int startJ, String word, boolean[][] visited, int m, int n) {
if (word.equals("")) {
return true;
}
for (int i = 0; i < directions.length - 1; i++) {
int nextX = startI + directions[i];
int nextY = startJ + directions[i + 1];
if (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && !visited[nextX][nextY] && board[nextX][nextY] == word.charAt(0)) {
visited[nextX][nextY] = true;
if (existByDfs(board, nextX, nextY, word.substring(1), visited, m, n)) {
return true;
}
//backtracking
visited[nextX][nextY] = false;
}
}
return false;
}
}
}