1
1
package com .fishercoder .solutions ;
2
2
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.
8
9
9
10
For example,
10
11
Given board =
16
17
17
18
word = "ABCCED", -> returns true,
18
19
word = "SEE", -> returns true,
19
- word = "ABCB", -> returns false.*/
20
+ word = "ABCB", -> returns false.
21
+ */
22
+
20
23
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 {
22
69
//credit: https://discuss.leetcode.com/topic/21142/my-java-solution
23
70
24
71
boolean [][] visited ;
@@ -58,48 +105,6 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
58
105
59
106
}
60
107
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
-
103
108
public static void main (String ... strings ) {
104
109
_79 test = new _79 ();
105
110
// char[][] board = new char[][]{
@@ -122,6 +127,7 @@ public static void main(String... strings) {
122
127
{'A' , 'D' , 'E' , 'E' },
123
128
};
124
129
String word = "ABCEFSADEESE" ;
125
- System .out .println (test .exist (board , word ));
130
+ Solution1 solution1 = new Solution1 ();
131
+ System .out .println (solution1 .exist (board , word ));
126
132
}
127
133
}
0 commit comments