@@ -19,31 +19,32 @@ public List<String> findWords(char[][] board, String[] words) {
19
19
}
20
20
21
21
private void dfs (TrieNode root ,char [][]board ,int i ,int j ,List <String >result ) {
22
- char c =board [i ][j ];
22
+ char tmp =board [i ][j ];
23
23
24
- if (c =='#' ||root .children [c -'a' ] ==null ) {
24
+ if (tmp =='#' ||root .children [tmp -'a' ] ==null ) {
25
25
return ;
26
26
}
27
27
28
- if (root .children [c -'a' ].word !=null ) {
29
- result .add (root .children [c -'a' ].word );
30
- root .children [c -'a' ].word =null ;//de-duplicate
28
+ if (root .children [tmp -'a' ].word !=null ) {
29
+ result .add (root .children [tmp -'a' ].word );
30
+ root .children [tmp -'a' ].word =null ;//de-duplicate
31
31
}
32
32
board [i ][j ] ='#' ;//mark it as visited to avoid cycles
33
33
if (i >0 ) {
34
- dfs (root .children [c -'a' ],board ,i -1 ,j ,result );
34
+ dfs (root .children [tmp -'a' ],board ,i -1 ,j ,result );
35
35
}
36
36
if (j >0 ) {
37
- dfs (root .children [c -'a' ],board ,i ,j -1 ,result );
37
+ dfs (root .children [tmp -'a' ],board ,i ,j -1 ,result );
38
38
}
39
39
if (i +1 <board .length ) {
40
- dfs (root .children [c -'a' ],board ,i +1 ,j ,result );
40
+ dfs (root .children [tmp -'a' ],board ,i +1 ,j ,result );
41
41
}
42
42
if (j +1 <board [0 ].length ) {
43
- dfs (root .children [c -'a' ],board ,i ,j +1 ,result );
43
+ dfs (root .children [tmp -'a' ],board ,i ,j +1 ,result );
44
44
}
45
45
46
- board [i ][j ] =c ;
46
+ //backtracking
47
+ board [i ][j ] =tmp ;
47
48
}
48
49
49
50
private TrieNode root ;
@@ -72,8 +73,7 @@ private TrieNode buildTrie(String[] words) {
72
73
73
74
public static class Solution2 {
74
75
public List <String >findWords (char [][]board ,String []words ) {
75
-
76
- List <String >result =new ArrayList ();
76
+ List <String >result ;
77
77
HashSet <String >set =new HashSet ();
78
78
for (String word :words ) {
79
79
for (int i =0 ;i <board .length ;i ++) {
@@ -88,22 +88,22 @@ public List<String> findWords(char[][] board, String[] words) {
88
88
return result ;
89
89
}
90
90
91
- private boolean search (char [][]board ,int i ,int j ,int count ,String word ) {
92
- if (count ==word .length ()) {
91
+ private boolean search (char [][]board ,int i ,int j ,int index ,String word ) {
92
+ if (index ==word .length ()) {
93
93
return true ;
94
94
}
95
95
96
- if (i <0 ||i >=board .length ||j <0 ||j >=board [0 ].length ||board [i ][j ] !=word .charAt (count )) {
96
+ if (i <0 ||i >=board .length ||j <0 ||j >=board [0 ].length ||board [i ][j ] !=word .charAt (index )) {
97
97
return false ;
98
98
}
99
99
100
100
char temp =board [i ][j ];
101
101
board [i ][j ] =' ' ;
102
102
103
- boolean foundWord =search (board ,i +1 ,j ,count +1 ,word )
104
- ||search (board ,i -1 ,j ,count +1 ,word )
105
- ||search (board ,i ,j +1 ,count +1 ,word )
106
- ||search (board ,i ,j -1 ,count +1 ,word );
103
+ boolean foundWord =search (board ,i +1 ,j ,index +1 ,word )
104
+ ||search (board ,i -1 ,j ,index +1 ,word )
105
+ ||search (board ,i ,j +1 ,index +1 ,word )
106
+ ||search (board ,i ,j -1 ,index +1 ,word );
107
107
board [i ][j ] =temp ;
108
108
return foundWord ;
109
109
}