Skip to content

Commit 204e107

Browse files
refactor 79
1 parent 8e375b3 commit 204e107

File tree

2 files changed

+0
-51
lines changed

2 files changed

+0
-51
lines changed

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,6 @@
33
public class _79 {
44

55
public static class Solution1 {
6-
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-
}
17-
}
18-
return false;
19-
}
20-
21-
final int[] dirs = new int[]{0, 1, 0, -1, 0};
22-
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-
}
45-
return result;
46-
}
47-
}
48-
49-
public static class Solution2 {
506
//credit: https://discuss.leetcode.com/topic/21142/my-java-solution
517

528
boolean[][] visited;

src/test/java/com/fishercoder/_79Test.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
public class _79Test {
1010
private static _79.Solution1 solution1;
11-
private static _79.Solution2 solution2;
1211
private static char[][] board;
1312

1413
@BeforeClass
1514
public static void setup() {
1615
solution1 = new _79.Solution1();
17-
solution2 = new _79.Solution2();
1816
}
1917

2018
@Test
@@ -25,7 +23,6 @@ public void test1() {
2523
{'A', 'D', 'E', 'E'},
2624
};
2725
assertEquals(true, solution1.exist(board, "ABCEFSADEESE"));
28-
assertEquals(true, solution2.exist(board, "ABCEFSADEESE"));
2926
}
3027

3128
@Test
@@ -36,13 +33,10 @@ public void test2() {
3633
{'A', 'D', 'E', 'E'},
3734
};
3835
assertEquals(true, solution1.exist(board, "ABCCED"));
39-
assertEquals(true, solution2.exist(board, "ABCCED"));
4036

4137
assertEquals(true, solution1.exist(board, "SEE"));
42-
assertEquals(true, solution2.exist(board, "SEE"));
4338

4439
assertEquals(false, solution1.exist(board, "ABCD"));
45-
assertEquals(false, solution2.exist(board, "ABCD"));
4640
}
4741

4842
@Test
@@ -52,7 +46,6 @@ public void test3() {
5246
{'a'},
5347
};
5448
assertEquals(false, solution1.exist(board, "aaa"));
55-
assertEquals(false, solution2.exist(board, "aaa"));
5649
}
5750

5851
}

0 commit comments

Comments
 (0)