@@ -19,31 +19,32 @@ public List<String> findWords(char[][] board, String[] words) {
1919 }
2020
2121private 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 ];
2323
24- if (c =='#' ||root .children [c -'a' ] ==null ) {
24+ if (tmp =='#' ||root .children [tmp -'a' ] ==null ) {
2525return ;
2626 }
2727
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
3131 }
3232board [i ][j ] ='#' ;//mark it as visited to avoid cycles
3333if (i >0 ) {
34- dfs (root .children [c -'a' ],board ,i -1 ,j ,result );
34+ dfs (root .children [tmp -'a' ],board ,i -1 ,j ,result );
3535 }
3636if (j >0 ) {
37- dfs (root .children [c -'a' ],board ,i ,j -1 ,result );
37+ dfs (root .children [tmp -'a' ],board ,i ,j -1 ,result );
3838 }
3939if (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 );
4141 }
4242if (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 );
4444 }
4545
46- board [i ][j ] =c ;
46+ //backtracking
47+ board [i ][j ] =tmp ;
4748 }
4849
4950private TrieNode root ;
@@ -72,8 +73,7 @@ private TrieNode buildTrie(String[] words) {
7273
7374public static class Solution2 {
7475public List <String >findWords (char [][]board ,String []words ) {
75-
76- List <String >result =new ArrayList ();
76+ List <String >result ;
7777HashSet <String >set =new HashSet ();
7878for (String word :words ) {
7979for (int i =0 ;i <board .length ;i ++) {
@@ -88,22 +88,22 @@ public List<String> findWords(char[][] board, String[] words) {
8888return result ;
8989 }
9090
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 ()) {
9393return true ;
9494 }
9595
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 )) {
9797return false ;
9898 }
9999
100100char temp =board [i ][j ];
101101board [i ][j ] =' ' ;
102102
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 );
107107board [i ][j ] =temp ;
108108return foundWord ;
109109 }