@@ -51,31 +51,36 @@ public boolean exist(char[][] board, String word) {
51
51
52
52
for (int i = 0 ; i < row ; i ++) {
53
53
for (int j = 0 ; j < col ; j ++) {
54
- if (board [i ][j ] == word .charAt (0 ) && search (board , i , j , word , 0 ) == true )
54
+ if (board [i ][j ] == word .charAt (0 ) && search (board , i , j , word , 0 ) == true ) {
55
55
return true ;
56
+ }
56
57
}
57
58
}
58
59
return false ;
59
60
}
60
61
61
62
private boolean search (char [][] board , int i , int j , String word , int index ) {
62
- if (index == word .length () - 1 )
63
+ if (index == word .length () - 1 ) {
63
64
return true ;
65
+ }
64
66
65
67
// store the visited char in temp variable
66
68
char temp = board [i ][j ];
67
69
board [i ][j ] = ' ' ;
68
- if (i > 0 && board [i - 1 ][j ] == word .charAt (index + 1 ) && search (board , i - 1 , j , word , index + 1 ) == true )
70
+ if (i > 0 && board [i - 1 ][j ] == word .charAt (index + 1 ) && search (board , i - 1 , j , word , index + 1 ) == true ) {
69
71
return true ;
70
- if (i < board .length - 1 && board [i + 1 ][j ] == word .charAt (index + 1 ) && search (board , i + 1 , j , word , index + 1 ) == true )
72
+ }
73
+ if (i < board .length - 1 && board [i + 1 ][j ] == word .charAt (index + 1 ) && search (board , i + 1 , j , word , index + 1 ) == true ) {
71
74
return true ;
75
+ }
72
76
73
- if (j > 0 && board [i ][j - 1 ] == word .charAt (index + 1 ) && search (board , i , j - 1 , word , index + 1 ) == true )
77
+ if (j > 0 && board [i ][j - 1 ] == word .charAt (index + 1 ) && search (board , i , j - 1 , word , index + 1 ) == true ) {
74
78
return true ;
79
+ }
75
80
76
-
77
- if (j < board [0 ].length - 1 && board [i ][j + 1 ] == word .charAt (index + 1 ) && search (board , i , j + 1 , word , index + 1 ) == true )
81
+ if (j < board [0 ].length - 1 && board [i ][j + 1 ] == word .charAt (index + 1 ) && search (board , i , j + 1 , word , index + 1 ) == true ) {
78
82
return true ;
83
+ }
79
84
80
85
board [i ][j ] = temp ;
81
86
return false ;
0 commit comments