3535 * - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word).
3636 */
3737public class WordSearch {
38- // private final int[] dx = {0, 0, 1, -1, 2, -2};
39- // private final int[] dy = {1, -1, 0, 0};
40- // private boolean[][] visited;
4138 private char [][] board ;
4239 private String word ;
4340
@@ -58,29 +55,9 @@ private boolean isValid(int x, int y) {
5855 *
5956 * @param x The current row index.
6057 * @param y The current column index.
61- * @param nextIdx The index of the next character in the word to be matched.
58+ * @param idx The index of the next character in the word to be matched.
6259 * @return True if a valid path is found to match the remaining characters of the word; false otherwise.
6360 */
64- // private boolean doDFS(int x, int y, int nextIdx) {
65- // visited[x][y] = true;
66- // if (nextIdx == word.length()) {
67- // return true;
68- // }
69- //
70- // for (int i = 0; i < 4; ++i) {
71- // int xi = x + dx[i];
72- // int yi = y + dy[i];
73- // if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) {
74- // boolean exists = doDFS(xi, yi, nextIdx + 1);
75- // if (exists) {
76- // return true;
77- // }
78- // }
79- // }
80- //
81- // visited[x][y] = false; // Backtrack
82- // return false;
83- // }
8461
8562 private boolean dfs (char [][] board , int x , int y , String word , int idx ) {
8663 if (idx == word .length ()) {
@@ -109,22 +86,6 @@ private boolean dfs(char[][] board, int x, int y, String word, int idx) {
10986 * @param word The target word to search for in the board.
11087 * @return True if the word exists in the board; false otherwise.
11188 */
112- // public boolean exist(char[][] board, String word) {
113- // this.board = board;
114- // this.word = word;
115- // for (int i = 0; i < board.length; ++i) {
116- // for (int j = 0; j < board[0].length; ++j) {
117- // if (board[i][j] == word.charAt(0)) {
118- // visited = new boolean[board.length][board[0].length];
119- // boolean exists = doDFS(i, j, 1);
120- // if (exists) {
121- // return true;
122- // }
123- // }
124- // }
125- // }
126- // return false;
127- // }
12889
12990 public boolean exist (char [][] board , String word ) {
13091
0 commit comments