You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* dfs2(row, col + 1, word, lvl + 1, visited, board) ||
142
+
* dfs2(row, col - 1, word, lvl + 1, visited, board);
143
+
*
144
+
* we can use below logic as well:
145
+
*
146
+
* for (int[] dir : dirs) {
147
+
* if (dfs_(board, y + dir[0], x + dir[1], idx + 1, word, visited)) {
148
+
* return true;
149
+
* }
150
+
* }
151
+
*/
152
+
/**
153
+
* 1) Detailed Explanation:
154
+
*
155
+
* Role of return true in the Loop
156
+
*
157
+
* 1. Backtracking Recursion:
158
+
* • The function backtrack is called recursively to explore possible paths in the grid.
159
+
* • Each recursive call either returns true if the word can be constructed from the current path or false if it cannot.
160
+
*
161
+
* 2. Returning Early:
162
+
* • As soon as one of the recursive calls returns true (indicating the word has been found), there is no need to continue exploring other directions. The word has already been successfully constructed.
163
+
*
164
+
* 3. Efficiency:
165
+
* • Exiting the loop early saves computation by avoiding exploration of unnecessary paths.
166
+
*
167
+
*
168
+
* 2) What Happens Without return true?
169
+
*
170
+
* -> If the return true is omitted, the function will:
171
+
* 1. Continue to check all remaining directions in the directions array, even after finding a valid path.
172
+
* 2. Complete all recursive calls, backtrack, and ultimately return false for the current recursion level, even if a valid path exists deeper in the recursion tree.
173
+
*
174
+
* -> This would result in the algorithm failing to detect that the word is present in the grid.
175
+
*
176
+
*
177
+
* 3) Example:
178
+
* Let’s consider a small grid:
179
+
*
180
+
* board = [
181
+
* ['A', 'B'],
182
+
* ['C', 'D']
183
+
* ];
184
+
* word = "AB";
185
+
*
186
+
*
187
+
* Start at (0, 0) (value A), matching the first character.
188
+
* • Check neighbors:
189
+
* • Move right to (0, 1) (value B), matching the second character. At this point, the word is found, so we return true.
190
+
*
191
+
* With return true:
192
+
* • When the recursive call to (0, 1) returns true, the loop exits immediately, and the function propagates true all the way up.
193
+
*
194
+
* Without return true:
195
+
* • Even after (0, 1) finds the word, the function continues checking other directions (down, left, up), wasting computation. Eventually, it backtracks, losing the valid result.
196
+
*
197
+
*
198
+
*
199
+
*
200
+
*
201
+
* 4) Conclusion
202
+
*
203
+
* Returning true immediately when a valid path is found is
204
+
* both correct and efficient. It skips redundant exploration
205
+
* and ensures that the recursion terminates as soon as the word is found.
206
+
* Other recursive logic is unaffected since the backtracking process
207
+
* stops as soon as we achieve the goal.
208
+
*
209
+
*/
210
+
for (int[] dir : dirs) {
154
211
if (dfs_(board, y + dir[0], x + dir[1], idx + 1, word, visited)) {
212
+
/** NOTE !!!
213
+
*
214
+
* need to return true IMMEDIATELY if a true solution is found
0 commit comments