Skip to content

Commit abec0f2

Browse files
[N-0] refactor 79
1 parent e1f5870 commit abec0f2

File tree

1 file changed

+56
-50
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+56
-50
lines changed

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

+56-50
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.fishercoder.solutions;
22

3-
/**Given a 2D board and a word, find if the word exists in the grid.
4-
5-
The word can be constructed from letters of sequentially adjacent cell,
6-
where "adjacent" cells are those horizontally or vertically neighboring.
7-
The same letter cell may not be used more than once.
3+
/**
4+
* 79. Word Search
5+
* Given a 2D board and a word, find if the word exists in the grid.
6+
* The word can be constructed from letters of sequentially adjacent cell,
7+
* where "adjacent" cells are those horizontally or vertically neighboring.
8+
* The same letter cell may not be used more than once.
89
910
For example,
1011
Given board =
@@ -16,9 +17,55 @@
1617
1718
word = "ABCCED", -> returns true,
1819
word = "SEE", -> returns true,
19-
word = "ABCB", -> returns false.*/
20+
word = "ABCB", -> returns false.
21+
*/
22+
2023
public class _79 {
21-
class SolutionOnDiscuss {
24+
public static class Solution1 {
25+
//I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
26+
public boolean exist(char[][] board, String word) {
27+
int m = board.length;
28+
int n = board[0].length;
29+
for (int i = 0; i < m; i++) {
30+
for (int j = 0; j < n; j++) {
31+
boolean[][] visited = new boolean[m][n];
32+
if (dfs(board, visited, i, j, word, 0)) {
33+
return true;
34+
}
35+
}
36+
}
37+
return false;
38+
}
39+
40+
final int[] dirs = new int[]{0, 1, 0, -1, 0};
41+
42+
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) {
43+
if (index >= word.length() || word.charAt(index) != board[row][col]) {
44+
return false;
45+
} else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) {
46+
visited[row][col] = true;
47+
return true;
48+
}
49+
visited[row][col] = true;//set it to true for this case
50+
boolean result = false;
51+
for (int i = 0; i < 4; i++) {
52+
int nextRow = row + dirs[i];
53+
int nextCol = col + dirs[i + 1];
54+
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
55+
continue;
56+
}
57+
result = dfs(board, visited, nextRow, nextCol, word, index + 1);
58+
if (result) {
59+
return result;
60+
} else {
61+
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
62+
}
63+
}
64+
return result;
65+
}
66+
}
67+
68+
public static class Solution2 {
2269
//credit: https://discuss.leetcode.com/topic/21142/my-java-solution
2370

2471
boolean[][] visited;
@@ -58,48 +105,6 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
58105

59106
}
60107

61-
//I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
62-
public boolean exist(char[][] board, String word) {
63-
int m = board.length;
64-
int n = board[0].length;
65-
for (int i = 0; i < m; i++) {
66-
for (int j = 0; j < n; j++) {
67-
boolean[][] visited = new boolean[m][n];
68-
if (dfs(board, visited, i, j, word, 0)) {
69-
return true;
70-
}
71-
}
72-
}
73-
return false;
74-
}
75-
76-
final int[] dirs = new int[]{0, 1, 0, -1, 0};
77-
78-
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) {
79-
if (index >= word.length() || word.charAt(index) != board[row][col]) {
80-
return false;
81-
} else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) {
82-
visited[row][col] = true;
83-
return true;
84-
}
85-
visited[row][col] = true;//set it to true for this case
86-
boolean result = false;
87-
for (int i = 0; i < 4; i++) {
88-
int nextRow = row + dirs[i];
89-
int nextCol = col + dirs[i + 1];
90-
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
91-
continue;
92-
}
93-
result = dfs(board, visited, nextRow, nextCol, word, index + 1);
94-
if (result) {
95-
return result;
96-
} else {
97-
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
98-
}
99-
}
100-
return result;
101-
}
102-
103108
public static void main(String... strings) {
104109
_79 test = new _79();
105110
// char[][] board = new char[][]{
@@ -122,6 +127,7 @@ public static void main(String... strings) {
122127
{'A', 'D', 'E', 'E'},
123128
};
124129
String word = "ABCEFSADEESE";
125-
System.out.println(test.exist(board, word));
130+
Solution1 solution1 = new Solution1();
131+
System.out.println(solution1.exist(board, word));
126132
}
127133
}

0 commit comments

Comments
 (0)