Skip to content

Commit bb4312c

Browse files
refactor 79
1 parent 8431b9d commit bb4312c

File tree

1 file changed

+37
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-58
lines changed

src/main/java/com/fishercoder/solutions/_79.java

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,49 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 79. Word Search
5-
*
6-
* Given a 2D board and a word, find if the word exists in the grid.
7-
* The word can be constructed from letters of sequentially adjacent cell,
8-
* where "adjacent" cells are those horizontally or vertically neighboring.
9-
* The same letter cell may not be used more than once.
10-
11-
For example,
12-
Given board =
13-
[
14-
['A','B','C','E'],
15-
['S','F','C','S'],
16-
['A','D','E','E']
17-
]
18-
19-
word = "ABCCED", -> returns true,
20-
word = "SEE", -> returns true,
21-
word = "ABCB", -> returns false.
22-
*/
23-
243
public class _79 {
254

265
public static class Solution1 {
276

28-
public boolean exist(char[][] board, String word) {
29-
int m = board.length;
30-
int n = board[0].length;
31-
boolean[][] visited = new boolean[m][n];
32-
for (int i = 0; i < m; i++) {
33-
for (int j = 0; j < n; j++) {
34-
if (dfs(board, visited, i, j, word, 0)) {
35-
return true;
7+
public boolean exist(char[][] board, String word) {
8+
int m = board.length;
9+
int n = board[0].length;
10+
boolean[][] visited = new boolean[m][n];
11+
for (int i = 0; i < m; i++) {
12+
for (int j = 0; j < n; j++) {
13+
if (dfs(board, visited, i, j, word, 0)) {
14+
return true;
15+
}
16+
}
3617
}
37-
}
18+
return false;
3819
}
39-
return false;
40-
}
4120

42-
final int[] dirs = new int[] {0, 1, 0, -1, 0};
21+
final int[] dirs = new int[]{0, 1, 0, -1, 0};
4322

44-
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) {
45-
if (index >= word.length() || word.charAt(index) != board[row][col]) {
46-
return false;
47-
} else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) {
48-
visited[row][col] = true;
49-
return true;
50-
}
51-
visited[row][col] = true;//set it to true for this case
52-
boolean result = false;
53-
for (int i = 0; i < 4; i++) {
54-
int nextRow = row + dirs[i];
55-
int nextCol = col + dirs[i + 1];
56-
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
57-
continue;
58-
}
59-
result = dfs(board, visited, nextRow, nextCol, word, index + 1);
60-
if (result) {
23+
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) {
24+
if (index >= word.length() || word.charAt(index) != board[row][col]) {
25+
return false;
26+
} else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) {
27+
visited[row][col] = true;
28+
return true;
29+
}
30+
visited[row][col] = true;//set it to true for this case
31+
boolean result = false;
32+
for (int i = 0; i < 4; i++) {
33+
int nextRow = row + dirs[i];
34+
int nextCol = col + dirs[i + 1];
35+
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
36+
continue;
37+
}
38+
result = dfs(board, visited, nextRow, nextCol, word, index + 1);
39+
if (result) {
40+
return result;
41+
} else {
42+
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
43+
}
44+
}
6145
return result;
62-
} else {
63-
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
64-
}
6546
}
66-
return result;
67-
}
6847
}
6948

7049
public static class Solution2 {
@@ -95,9 +74,9 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
9574
}
9675
visited[i][j] = true;
9776
if (search(board, word, i + 1, j, pos + 1)
98-
|| search(board, word, i - 1, j, pos + 1)
99-
|| search(board, word, i, j + 1, pos + 1)
100-
|| search(board, word, i, j - 1, pos + 1)) {
77+
|| search(board, word, i - 1, j, pos + 1)
78+
|| search(board, word, i, j + 1, pos + 1)
79+
|| search(board, word, i, j - 1, pos + 1)) {
10180
return true;
10281
}
10382

0 commit comments

Comments
 (0)