From 81c46ea572b5d2ca13e4eee5ae83c301e87ca9f2 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 13:00:42 +0000 Subject: [PATCH 1/9] Modifying space complexity to O(1). --- .../backtracking/WordSearch.java | 96 ++++++++++++++----- 1 file changed, 72 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index 174ca90ccaab..d8f7d39b392e 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -35,7 +35,7 @@ * - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word). */ public class WordSearch { - private final int[] dx = {0, 0, 1, -1}; + private final int[] dx = {0, 0, 1, -1, 2, -2}; private final int[] dy = {1, -1, 0, 0}; private boolean[][] visited; private char[][] board; @@ -61,25 +61,49 @@ private boolean isValid(int x, int y) { * @param nextIdx The index of the next character in the word to be matched. * @return True if a valid path is found to match the remaining characters of the word; false otherwise. */ - private boolean doDFS(int x, int y, int nextIdx) { - visited[x][y] = true; - if (nextIdx == word.length()) { - return true; - } +// private boolean doDFS(int x, int y, int nextIdx) { +// visited[x][y] = true; +// if (nextIdx == word.length()) { +// return true; +// } +// +// for (int i = 0; i < 4; ++i) { +// int xi = x + dx[i]; +// int yi = y + dy[i]; +// if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) { +// boolean exists = doDFS(xi, yi, nextIdx + 1); +// if (exists) { +// return true; +// } +// } +// } +// +// visited[x][y] = false; // Backtrack +// return false; +// } - for (int i = 0; i < 4; ++i) { - int xi = x + dx[i]; - int yi = y + dy[i]; - if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) { - boolean exists = doDFS(xi, yi, nextIdx + 1); - if (exists) { - return true; - } - } + + private boolean dfs(char[][] board, int x, int y, String word, int idx) { + if (idx == word.length()) return true; + + if (x < 0 || y < 0 || + x >= board.length || y >= board[0].length || + board[x][y] != word.charAt(idx)) { + return false; } - visited[x][y] = false; // Backtrack - return false; + char temp = board[x][y]; + board[x][y] = '#'; + + 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); + + board[x][y] = temp; + + return found; } /** @@ -90,20 +114,44 @@ private boolean doDFS(int x, int y, int nextIdx) { * @param word The target word to search for in the board. * @return True if the word exists in the board; false otherwise. */ +// public boolean exist(char[][] board, String word) { +// this.board = board; +// this.word = word; +// for (int i = 0; i < board.length; ++i) { +// for (int j = 0; j < board[0].length; ++j) { +// if (board[i][j] == word.charAt(0)) { +// visited = new boolean[board.length][board[0].length]; +// boolean exists = doDFS(i, j, 1); +// if (exists) { +// return true; +// } +// } +// } +// } +// return false; +// } + + + public boolean exist(char[][] board, String word) { - this.board = board; - this.word = word; - for (int i = 0; i < board.length; ++i) { - for (int j = 0; j < board[0].length; ++j) { + + int m = board.length; + int n = board[0].length; + + + // DFS search + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (board[i][j] == word.charAt(0)) { - visited = new boolean[board.length][board[0].length]; - boolean exists = doDFS(i, j, 1); - if (exists) { + if (dfs(board, i, j, word, 0)) { return true; } } + } } + return false; } } From 44fc523490a94d59e13246615ec909cf08d75766 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 14:36:22 +0000 Subject: [PATCH 2/9] Fix formatting using clang-format. --- .../backtracking/WordSearch.java | 87 ++++++++----------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index d8f7d39b392e..eff0af561266 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -61,45 +61,38 @@ private boolean isValid(int x, int y) { * @param nextIdx The index of the next character in the word to be matched. * @return True if a valid path is found to match the remaining characters of the word; false otherwise. */ -// private boolean doDFS(int x, int y, int nextIdx) { -// visited[x][y] = true; -// if (nextIdx == word.length()) { -// return true; -// } -// -// for (int i = 0; i < 4; ++i) { -// int xi = x + dx[i]; -// int yi = y + dy[i]; -// if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) { -// boolean exists = doDFS(xi, yi, nextIdx + 1); -// if (exists) { -// return true; -// } -// } -// } -// -// visited[x][y] = false; // Backtrack -// return false; -// } - + // private boolean doDFS(int x, int y, int nextIdx) { + // visited[x][y] = true; + // if (nextIdx == word.length()) { + // return true; + // } + // + // for (int i = 0; i < 4; ++i) { + // int xi = x + dx[i]; + // int yi = y + dy[i]; + // if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) { + // boolean exists = doDFS(xi, yi, nextIdx + 1); + // if (exists) { + // return true; + // } + // } + // } + // + // visited[x][y] = false; // Backtrack + // return false; + // } private boolean dfs(char[][] board, int x, int y, String word, int idx) { if (idx == word.length()) return true; - if (x < 0 || y < 0 || - x >= board.length || y >= board[0].length || - board[x][y] != word.charAt(idx)) { + if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) { return false; } char temp = board[x][y]; board[x][y] = '#'; - 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); + 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); board[x][y] = temp; @@ -114,31 +107,28 @@ private boolean dfs(char[][] board, int x, int y, String word, int idx) { * @param word The target word to search for in the board. * @return True if the word exists in the board; false otherwise. */ -// public boolean exist(char[][] board, String word) { -// this.board = board; -// this.word = word; -// for (int i = 0; i < board.length; ++i) { -// for (int j = 0; j < board[0].length; ++j) { -// if (board[i][j] == word.charAt(0)) { -// visited = new boolean[board.length][board[0].length]; -// boolean exists = doDFS(i, j, 1); -// if (exists) { -// return true; -// } -// } -// } -// } -// return false; -// } - - + // public boolean exist(char[][] board, String word) { + // this.board = board; + // this.word = word; + // for (int i = 0; i < board.length; ++i) { + // for (int j = 0; j < board[0].length; ++j) { + // if (board[i][j] == word.charAt(0)) { + // visited = new boolean[board.length][board[0].length]; + // boolean exists = doDFS(i, j, 1); + // if (exists) { + // return true; + // } + // } + // } + // } + // return false; + // } public boolean exist(char[][] board, String word) { int m = board.length; int n = board[0].length; - // DFS search for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { @@ -148,7 +138,6 @@ public boolean exist(char[][] board, String word) { return true; } } - } } From 2f170cc459edc584d32d8afb7002835752a239b0 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:04:51 +0000 Subject: [PATCH 3/9] Fix checkstyle violations. --- src/main/java/com/thealgorithms/backtracking/WordSearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index eff0af561266..d76f6ae50016 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -83,7 +83,7 @@ private boolean isValid(int x, int y) { // } private boolean dfs(char[][] board, int x, int y, String word, int idx) { - if (idx == word.length()) return true; + if (idx == word.length()) { return true; } if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) { return false; From f97d710bb440fb033ed9697dda28a841daab7df8 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:12:03 +0000 Subject: [PATCH 4/9] Fix checkstyle violations and code formatting. --- src/main/java/com/thealgorithms/backtracking/WordSearch.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index d76f6ae50016..e61cefa489c2 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -83,7 +83,9 @@ private boolean isValid(int x, int y) { // } private boolean dfs(char[][] board, int x, int y, String word, int idx) { - if (idx == word.length()) { return true; } + if (idx == word.length()) { + return true; + } if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) { return false; From c3a1bccc52bfa4f16edb2b6e18e358f5788a71c6 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:23:04 +0000 Subject: [PATCH 5/9] Remove unused fields reported by SpotBugs. --- .../java/com/thealgorithms/backtracking/WordSearch.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index e61cefa489c2..73a8b54e155a 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -35,9 +35,9 @@ * - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word). */ public class WordSearch { - private final int[] dx = {0, 0, 1, -1, 2, -2}; - private final int[] dy = {1, -1, 0, 0}; - private boolean[][] visited; +// private final int[] dx = {0, 0, 1, -1, 2, -2}; +// private final int[] dy = {1, -1, 0, 0}; +// private boolean[][] visited; private char[][] board; private String word; From 8aaf33a219836623e091ade51c2131cfe19b75a8 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:31:18 +0000 Subject: [PATCH 6/9] Remove unused fields and comments. --- .../backtracking/WordSearch.java | 41 +------------------ 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index 73a8b54e155a..3032afe8c387 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -35,9 +35,6 @@ * - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word). */ public class WordSearch { -// private final int[] dx = {0, 0, 1, -1, 2, -2}; -// private final int[] dy = {1, -1, 0, 0}; -// private boolean[][] visited; private char[][] board; private String word; @@ -58,29 +55,9 @@ private boolean isValid(int x, int y) { * * @param x The current row index. * @param y The current column index. - * @param nextIdx The index of the next character in the word to be matched. + * @param idx The index of the next character in the word to be matched. * @return True if a valid path is found to match the remaining characters of the word; false otherwise. */ - // private boolean doDFS(int x, int y, int nextIdx) { - // visited[x][y] = true; - // if (nextIdx == word.length()) { - // return true; - // } - // - // for (int i = 0; i < 4; ++i) { - // int xi = x + dx[i]; - // int yi = y + dy[i]; - // if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) { - // boolean exists = doDFS(xi, yi, nextIdx + 1); - // if (exists) { - // return true; - // } - // } - // } - // - // visited[x][y] = false; // Backtrack - // return false; - // } private boolean dfs(char[][] board, int x, int y, String word, int idx) { if (idx == word.length()) { @@ -109,22 +86,6 @@ private boolean dfs(char[][] board, int x, int y, String word, int idx) { * @param word The target word to search for in the board. * @return True if the word exists in the board; false otherwise. */ - // public boolean exist(char[][] board, String word) { - // this.board = board; - // this.word = word; - // for (int i = 0; i < board.length; ++i) { - // for (int j = 0; j < board[0].length; ++j) { - // if (board[i][j] == word.charAt(0)) { - // visited = new boolean[board.length][board[0].length]; - // boolean exists = doDFS(i, j, 1); - // if (exists) { - // return true; - // } - // } - // } - // } - // return false; - // } public boolean exist(char[][] board, String word) { From 9fde3c5e519f54dd96bf3ed3617a1ec405360a95 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:41:08 +0000 Subject: [PATCH 7/9] Remove unused field reported by SpotBugs. --- .../com/thealgorithms/backtracking/WordSearch.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index 3032afe8c387..8fa899288e0b 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -35,19 +35,6 @@ * - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word). */ public class WordSearch { - private char[][] board; - private String word; - - /** - * Checks if the given (x, y) coordinates are valid positions in the board. - * - * @param x The row index. - * @param y The column index. - * @return True if the coordinates are within the bounds of the board; false otherwise. - */ - private boolean isValid(int x, int y) { - return x >= 0 && x < board.length && y >= 0 && y < board[0].length; - } /** * Performs Depth First Search (DFS) from the cell (x, y) From 5262c3169db75605f42f9ba716ac5183448bd7d8 Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 15:53:44 +0000 Subject: [PATCH 8/9] Fix PMD collapsible if statement. --- src/main/java/com/thealgorithms/backtracking/WordSearch.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index 8fa899288e0b..d69d39e94359 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -83,10 +83,8 @@ public boolean exist(char[][] board, String word) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { - if (board[i][j] == word.charAt(0)) { - if (dfs(board, i, j, word, 0)) { + if (board[i][j] == word.charAt(0) && dfs(board, i, j, word, 0)) { return true; - } } } } From 591b36fcf26b67c7b595e41278acaf3cc933f8df Mon Sep 17 00:00:00 2001 From: maryamh12 Date: Thu, 12 Mar 2026 16:17:53 +0000 Subject: [PATCH 9/9] Fix indentation to satisfy clang-format. --- src/main/java/com/thealgorithms/backtracking/WordSearch.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/WordSearch.java b/src/main/java/com/thealgorithms/backtracking/WordSearch.java index d69d39e94359..452f17b6ace6 100644 --- a/src/main/java/com/thealgorithms/backtracking/WordSearch.java +++ b/src/main/java/com/thealgorithms/backtracking/WordSearch.java @@ -82,9 +82,8 @@ public boolean exist(char[][] board, String word) { // DFS search for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { - if (board[i][j] == word.charAt(0) && dfs(board, i, j, word, 0)) { - return true; + return true; } } }