Skip to content

Commit 24c2bea

Browse files
Improve space complexity to O(1) in WordSearch. (#7308)
* Modifying space complexity to O(1). * Fix formatting using clang-format. * Fix checkstyle violations. * Fix checkstyle violations and code formatting. * Remove unused fields reported by SpotBugs. * Remove unused fields and comments. * Remove unused field reported by SpotBugs. * Fix PMD collapsible if statement. * Fix indentation to satisfy clang-format. --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent 8bbd090 commit 24c2bea

File tree

1 file changed

+25
-41
lines changed

1 file changed

+25
-41
lines changed

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

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,51 +35,34 @@
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};
39-
private final int[] dy = {1, -1, 0, 0};
40-
private boolean[][] visited;
41-
private char[][] board;
42-
private String word;
43-
44-
/**
45-
* Checks if the given (x, y) coordinates are valid positions in the board.
46-
*
47-
* @param x The row index.
48-
* @param y The column index.
49-
* @return True if the coordinates are within the bounds of the board; false otherwise.
50-
*/
51-
private boolean isValid(int x, int y) {
52-
return x >= 0 && x < board.length && y >= 0 && y < board[0].length;
53-
}
5438

5539
/**
5640
* Performs Depth First Search (DFS) from the cell (x, y)
5741
* to search for the next character in the word.
5842
*
5943
* @param x The current row index.
6044
* @param y The current column index.
61-
* @param nextIdx The index of the next character in the word to be matched.
45+
* @param idx The index of the next character in the word to be matched.
6246
* @return True if a valid path is found to match the remaining characters of the word; false otherwise.
6347
*/
64-
private boolean doDFS(int x, int y, int nextIdx) {
65-
visited[x][y] = true;
66-
if (nextIdx == word.length()) {
48+
49+
private boolean dfs(char[][] board, int x, int y, String word, int idx) {
50+
if (idx == word.length()) {
6751
return true;
6852
}
6953

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-
}
54+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) {
55+
return false;
7956
}
8057

81-
visited[x][y] = false; // Backtrack
82-
return false;
58+
char temp = board[x][y];
59+
board[x][y] = '#';
60+
61+
boolean found = 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+
return found;
8366
}
8467

8568
/**
@@ -90,20 +73,21 @@ private boolean doDFS(int x, int y, int nextIdx) {
9073
* @param word The target word to search for in the board.
9174
* @return True if the word exists in the board; false otherwise.
9275
*/
76+
9377
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) {
98-
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) {
102-
return true;
103-
}
78+
79+
int m = board.length;
80+
int n = board[0].length;
81+
82+
// DFS search
83+
for (int i = 0; i < m; i++) {
84+
for (int j = 0; j < n; j++) {
85+
if (board[i][j] == word.charAt(0) && dfs(board, i, j, word, 0)) {
86+
return true;
10487
}
10588
}
10689
}
90+
10791
return false;
10892
}
10993
}

0 commit comments

Comments
 (0)