Skip to content

Commit 44fc523

Browse files
committed
Fix formatting using clang-format.
1 parent 81c46ea commit 44fc523

File tree

1 file changed

+38
-49
lines changed

1 file changed

+38
-49
lines changed

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

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,38 @@ 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-
// }
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-
// }
84-
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+
// }
8584

8685
private boolean dfs(char[][] board, int x, int y, String word, int idx) {
8786
if (idx == word.length()) return true;
8887

89-
if (x < 0 || y < 0 ||
90-
x >= board.length || y >= board[0].length ||
91-
board[x][y] != word.charAt(idx)) {
88+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) {
9289
return false;
9390
}
9491

9592
char temp = board[x][y];
9693
board[x][y] = '#';
9794

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);
95+
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);
10396

10497
board[x][y] = temp;
10598

@@ -114,31 +107,28 @@ private boolean dfs(char[][] board, int x, int y, String word, int idx) {
114107
* @param word The target word to search for in the board.
115108
* @return True if the word exists in the board; false otherwise.
116109
*/
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-
110+
// public boolean exist(char[][] board, String word) {
111+
// this.board = board;
112+
// this.word = word;
113+
// for (int i = 0; i < board.length; ++i) {
114+
// for (int j = 0; j < board[0].length; ++j) {
115+
// if (board[i][j] == word.charAt(0)) {
116+
// visited = new boolean[board.length][board[0].length];
117+
// boolean exists = doDFS(i, j, 1);
118+
// if (exists) {
119+
// return true;
120+
// }
121+
// }
122+
// }
123+
// }
124+
// return false;
125+
// }
135126

136127
public boolean exist(char[][] board, String word) {
137128

138129
int m = board.length;
139130
int n = board[0].length;
140131

141-
142132
// DFS search
143133
for (int i = 0; i < m; i++) {
144134
for (int j = 0; j < n; j++) {
@@ -148,7 +138,6 @@ public boolean exist(char[][] board, String word) {
148138
return true;
149139
}
150140
}
151-
152141
}
153142
}
154143

0 commit comments

Comments
 (0)