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
if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) {
74
-
booleanexists = doDFS(xi, yi, nextIdx + 1);
75
-
if (exists) {
76
-
returntrue;
77
-
}
78
-
}
54
+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) {
55
+
returnfalse;
79
56
}
80
57
81
-
visited[x][y] = false; // Backtrack
82
-
returnfalse;
58
+
chartemp = board[x][y];
59
+
board[x][y] = '#';
60
+
61
+
booleanfound = dfs(board, x + 1, y, word, idx + 1) || dfs(board, x - 1, y, word, idx + 1) || dfs(board, x, y + 1, word, idx + 1) || dfs(board, x, y - 1, word, idx + 1);
62
+
63
+
board[x][y] = temp;
64
+
65
+
returnfound;
83
66
}
84
67
85
68
/**
@@ -90,20 +73,21 @@ private boolean doDFS(int x, int y, int nextIdx) {
90
73
* @param word The target word to search for in the board.
91
74
* @return True if the word exists in the board; false otherwise.
0 commit comments