Skip to content

Commit 81c46ea

Browse files
committed
Modifying space complexity to O(1).
1 parent 5e06b15 commit 81c46ea

File tree

1 file changed

+72
-24
lines changed

1 file changed

+72
-24
lines changed

src/main/java/com/thealgorithms/backtracking/WordSearch.java

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word).
3636
*/
3737
public class WordSearch {
38-
private final int[] dx = {0, 0, 1, -1};
38+
private final int[] dx = {0, 0, 1, -1, 2, -2};
3939
private final int[] dy = {1, -1, 0, 0};
4040
private boolean[][] visited;
4141
private char[][] board;
@@ -61,25 +61,49 @@ private boolean isValid(int x, int y) {
6161
* @param nextIdx The index of the next character in the word to be matched.
6262
* @return True if a valid path is found to match the remaining characters of the word; false otherwise.
6363
*/
64-
private boolean doDFS(int x, int y, int nextIdx) {
65-
visited[x][y] = true;
66-
if (nextIdx == word.length()) {
67-
return true;
68-
}
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+
// }
6984

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-
}
85+
86+
private boolean dfs(char[][] board, int x, int y, String word, int idx) {
87+
if (idx == word.length()) return true;
88+
89+
if (x < 0 || y < 0 ||
90+
x >= board.length || y >= board[0].length ||
91+
board[x][y] != word.charAt(idx)) {
92+
return false;
7993
}
8094

81-
visited[x][y] = false; // Backtrack
82-
return false;
95+
char temp = board[x][y];
96+
board[x][y] = '#';
97+
98+
boolean found =
99+
dfs(board, x + 1, y, word, idx + 1) ||
100+
dfs(board, x - 1, y, word, idx + 1) ||
101+
dfs(board, x, y + 1, word, idx + 1) ||
102+
dfs(board, x, y - 1, word, idx + 1);
103+
104+
board[x][y] = temp;
105+
106+
return found;
83107
}
84108

85109
/**
@@ -90,20 +114,44 @@ private boolean doDFS(int x, int y, int nextIdx) {
90114
* @param word The target word to search for in the board.
91115
* @return True if the word exists in the board; false otherwise.
92116
*/
117+
// public boolean exist(char[][] board, String word) {
118+
// this.board = board;
119+
// this.word = word;
120+
// for (int i = 0; i < board.length; ++i) {
121+
// for (int j = 0; j < board[0].length; ++j) {
122+
// if (board[i][j] == word.charAt(0)) {
123+
// visited = new boolean[board.length][board[0].length];
124+
// boolean exists = doDFS(i, j, 1);
125+
// if (exists) {
126+
// return true;
127+
// }
128+
// }
129+
// }
130+
// }
131+
// return false;
132+
// }
133+
134+
135+
93136
public boolean exist(char[][] board, String word) {
94-
this.board = board;
95-
this.word = word;
96-
for (int i = 0; i < board.length; ++i) {
97-
for (int j = 0; j < board[0].length; ++j) {
137+
138+
int m = board.length;
139+
int n = board[0].length;
140+
141+
142+
// DFS search
143+
for (int i = 0; i < m; i++) {
144+
for (int j = 0; j < n; j++) {
145+
98146
if (board[i][j] == word.charAt(0)) {
99-
visited = new boolean[board.length][board[0].length];
100-
boolean exists = doDFS(i, j, 1);
101-
if (exists) {
147+
if (dfs(board, i, j, word, 0)) {
102148
return true;
103149
}
104150
}
151+
105152
}
106153
}
154+
107155
return false;
108156
}
109157
}

0 commit comments

Comments
 (0)