Skip to content

Commit

Permalink
update 79 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 4, 2024
1 parent 4b1ad3a commit dbfb13b
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class WordSearch {


// V1
// IDEA : DFS
// IDEA : DFS + BACKTRACK
// https://leetcode.com/problems/word-search/solutions/4791515/java-easy-solution-dfs-backtracking/
public boolean exist(char[][] board, String word) {
int n = board.length;
Expand Down Expand Up @@ -95,23 +95,79 @@ public boolean dfs(int row, int col, String word, int lvl, boolean[][] visited,
visited[row][col] = true;

boolean didFindNextCharacter =
dfs(row + 1, col, word, lvl + 1, visited, board)||
dfs(row - 1, col, word, lvl + 1, visited, board)||
dfs(row, col + 1, word, lvl + 1, visited, board)||
dfs(row, col - 1, word, lvl + 1, visited, board);
dfs(row + 1, col, word, lvl + 1, visited, board) ||
dfs(row - 1, col, word, lvl + 1, visited, board) ||
dfs(row, col + 1, word, lvl + 1, visited, board) ||
dfs(row, col - 1, word, lvl + 1, visited, board);

visited[row][col] = false; // Backtrack
return didFindNextCharacter;
}

// V1'
// IDEA : V1 variation
public boolean exist_1(char[][] board, String word) {
int n = board.length;
int m = board[0].length;

boolean[][] visited = new boolean[n][m];

for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(board[row][col] == word.charAt(0)){
if(dfs_1(row, col, word, 0, visited, board)){
return true;
}
}
}
}
return false;
}

public boolean dfs_1(int row, int col, String word, int lvl, boolean[][] visited, char[][] board){
int n = board.length;
int m = board[0].length;

if(lvl == word.length()){
return true;
}

if(row < 0 || row >= n || col < 0 || col >= m || visited[row][col] || board[row][col] != word.charAt(lvl)){
return false;
}

visited[row][col] = true;

if (dfs_1(row + 1, col, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row - 1, col, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row, col + 1, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}
if (dfs_1(row, col - 1, word, lvl + 1, visited, board)) {
visited[row][col] = false; // Backtrack
return true;
}

visited[row][col] = false; // Backtrack
return false;
}


// V1''
// IDEA : BACKTRACK
// https://leetcode.com/problems/word-search/editorial/
private char[][] board;
private int ROWS;
private int COLS;

public boolean exist_1(char[][] board, String word) {
public boolean exist_1_(char[][] board, String word) {
this.board = board;
this.ROWS = board.length;
this.COLS = board[0].length;
Expand Down

0 comments on commit dbfb13b

Please sign in to comment.