-
Notifications
You must be signed in to change notification settings - Fork 43
/
WordSearch.java
390 lines (312 loc) · 11.7 KB
/
WordSearch.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package LeetCodeJava.BackTrack;
// https://leetcode.com/problems/word-search/
public class WordSearch {
// V0
// IDEA : DFS + BACKTRACK
public boolean exist(char[][] board, String word) {
int n = board.length;
int m = board[0].length;
/** NOTE !!! we define visited */
boolean[][] visited = new boolean[n][m];
for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(board[row][col] == word.charAt(0)){
if(dfs(row, col, word, 0, visited, board)){
return true;
}
}
}
}
return false;
}
public boolean dfs(int row, int col, String word, int lvl, boolean[][] visited, char[][] board){
int n = board.length;
int m = board[0].length;
if(lvl == word.length()){
return true;
}
if(row < 0 || row >= n || col < 0 || col >= m || visited[row][col] || board[row][col] != word.charAt(lvl)){
return false;
}
/** NOTE !!! mark cur x, y as visited */
visited[row][col] = true;
if ( dfs(row + 1, col, word, lvl + 1, visited, board) ||
dfs(row - 1, col, word, lvl + 1, visited, board) ||
dfs(row, col + 1, word, lvl + 1, visited, board) ||
dfs(row, col - 1, word, lvl + 1, visited, board) ){
return true;
}
/** NOTE !!! Backtrack : undo cur x, y as visited */
visited[row][col] = false; // Backtrack
return false;
}
// V0'
// IDEA : DFS + BACKTRACK (modified by GPT)
public boolean exist_0(char[][] board, String word) {
if (board == null || board.length == 0) {
return false;
}
int l = board.length;
int w = board[0].length;
boolean[][] visited = new boolean[l][w];
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
if (dfs_(board, i, j, 0, word, visited)) {
return true;
}
}
}
return false;
}
private boolean dfs_(char[][] board, int y, int x, int idx, String word, boolean[][] visited) {
if (idx == word.length()) {
return true;
}
int l = board.length;
int w = board[0].length;
if (y < 0 || y >= l || x < 0 || x >= w || visited[y][x] || board[y][x] != word.charAt(idx)) {
return false;
}
/** NOTE !!! we update visited on x, y here */
visited[y][x] = true;
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
/**
* NOTE !!!
*
* instead of below structure:
*
* boolean didFindNextCharacter =
* dfs2(row + 1, col, word, lvl + 1, visited, board) ||
* dfs2(row - 1, col, word, lvl + 1, visited, board) ||
* dfs2(row, col + 1, word, lvl + 1, visited, board) ||
* dfs2(row, col - 1, word, lvl + 1, visited, board);
*
* we can use below logic as well:
*
* for (int[] dir : dirs) {
* if (dfs_(board, y + dir[0], x + dir[1], idx + 1, word, visited)) {
* return true;
* }
* }
*
*/
for (int[] dir : dirs) {
if (dfs_(board, y + dir[0], x + dir[1], idx + 1, word, visited)) {
return true;
}
}
/** NOTE !!! we undo (backtrack) updated x, y here */
visited[y][x] = false;
return false;
}
// V0''
// IDEA : modified version of v0' (gpt)
public boolean exist_0_1(char[][] board, String word) {
if (board == null || board.length == 0) {
return false;
}
int l = board.length;
int w = board[0].length;
boolean[][] visited = new boolean[l][w];
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
if (dfs_0_1(board, i, j, 0, word, visited)) {
return true;
}
}
}
return false;
}
private boolean dfs_0_1(char[][] board, int y, int x, int idx, String word, boolean[][] visited) {
if (idx == word.length()) {
return true;
}
int l = board.length;
int w = board[0].length;
if (y < 0 || y >= l || x < 0 || x >= w || visited[y][x] || board[y][x] != word.charAt(idx)) {
return false;
}
// Mark this cell as visited
visited[y][x] = true;
// Directions array for exploring up, down, left, and right
int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
/** NOTE !!! init found flag, for tracking found status */
boolean found = false; // Variable to store if the word is found in any direction
// Explore all four directions
for (int[] dir : dirs) {
if (dfs_0_1(board, y + dir[0], x + dir[1], idx + 1, word, visited)) {
/** NOTE !!! if found, update found status */
found = true;
/** NOTE !!!
*
* if found, break the loop,
* then will go to final line in dfs (e.g. return found;),
* and return found status directly
*/
break; // We can break out of the loop if we found the word in any direction
}
}
// Backtrack: unmark this cell as visited
visited[y][x] = false;
return found;
}
// V1
// IDEA : DFS + BACKTRACK
// https://leetcode.com/problems/word-search/solutions/4791515/java-easy-solution-dfs-backtracking/
public boolean exist2(char[][] board, String word) {
int n = board.length;
int m = board[0].length;
boolean[][] visited = new boolean[n][m];
for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(board[row][col] == word.charAt(0)){
if(dfs2(row, col, word, 0, visited, board)){
return true;
}
}
}
}
return false;
}
public boolean dfs2(int row, int col, String word, int lvl, boolean[][] visited, char[][] board){
int n = board.length;
int m = board[0].length;
if(lvl == word.length()){
return true;
}
if(row < 0 || row >= n || col < 0 || col >= m || visited[row][col] || board[row][col] != word.charAt(lvl)){
return false;
}
visited[row][col] = true;
boolean didFindNextCharacter =
dfs2(row + 1, col, word, lvl + 1, visited, board) ||
dfs2(row - 1, col, word, lvl + 1, visited, board) ||
dfs2(row, col + 1, word, lvl + 1, visited, board) ||
dfs2(row, col - 1, word, lvl + 1, visited, board);
visited[row][col] = false; // Backtrack
return didFindNextCharacter;
}
// V1'
// IDEA : V1 variation
public boolean exist_1(char[][] board, String word) {
int n = board.length;
int m = board[0].length;
boolean[][] visited = new boolean[n][m];
for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(board[row][col] == word.charAt(0)){
if(dfs_1(row, col, word, 0, visited, board)){
return true;
}
}
}
}
return false;
}
public boolean dfs_1(int row, int col, String word, int lvl, boolean[][] visited, char[][] board){
int n = board.length;
int m = board[0].length;
if(lvl == word.length()){
return true;
}
if(row < 0 || row >= n || col < 0 || col >= m || visited[row][col] || board[row][col] != word.charAt(lvl)){
return false;
}
visited[row][col] = true;
if (dfs_1(row + 1, col, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row - 1, col, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row, col + 1, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row, col - 1, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
visited[row][col] = false; // Backtrack
return false;
}
// V1''
// IDEA : BACKTRACK
// https://leetcode.com/problems/word-search/editorial/
private char[][] board;
private int ROWS;
private int COLS;
public boolean exist_1_(char[][] board, String word) {
this.board = board;
this.ROWS = board.length;
this.COLS = board[0].length;
for (int row = 0; row < this.ROWS; ++row)
for (int col = 0; col < this.COLS; ++col)
if (this.backtrack(row, col, word, 0))
return true;
return false;
}
protected boolean backtrack(int row, int col, String word, int index) {
/* Step 1). check the bottom case. */
if (index >= word.length())
return true;
/* Step 2). Check the boundaries. */
if (row < 0 || row == this.ROWS || col < 0 || col == this.COLS
|| this.board[row][col] != word.charAt(index))
return false;
/* Step 3). explore the neighbors in DFS */
boolean ret = false;
// mark the path before the next exploration
this.board[row][col] = '#';
int[] rowOffsets = {0, 1, 0, -1};
int[] colOffsets = {1, 0, -1, 0};
for (int d = 0; d < 4; ++d) {
ret = this.backtrack(row + rowOffsets[d], col + colOffsets[d], word, index + 1);
if (ret)
break;
}
/* Step 4). clean up and return the result. */
this.board[row][col] = word.charAt(index);
return ret;
}
// V1-1
// IDEA :
// https://leetcode.com/problems/word-search/editorial/
private char[][] board2;
private int ROWS2;
private int COLS2;
public boolean exist_1_2(char[][] board, String word) {
this.board2 = board;
this.ROWS2 = board.length;
this.COLS2 = board[0].length;
for (int row = 0; row < this.ROWS2; ++row)
for (int col = 0; col < this.COLS2; ++col)
if (this.backtrack2(row, col, word, 0))
return true;
return false;
}
protected boolean backtrack2(int row, int col, String word, int index) {
/* Step 1). check the bottom case. */
if (index >= word.length())
return true;
/* Step 2). Check the boundaries. */
if (row < 0 || row == this.ROWS || col < 0 || col == this.COLS
|| this.board[row][col] != word.charAt(index))
return false;
/* Step 3). explore the neighbors in DFS */
// mark the path before the next exploration
this.board[row][col] = '#';
int[] rowOffsets = {0, 1, 0, -1};
int[] colOffsets = {1, 0, -1, 0};
for (int d = 0; d < 4; ++d) {
if (this.backtrack2(row + rowOffsets[d], col + colOffsets[d], word, index + 1))
// return without cleanup
return true;
}
/* Step 4). clean up and return the result. */
this.board[row][col] = word.charAt(index);
return false;
}
}