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 9, 2024
1 parent bf4b867 commit 9f55607
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 59 deletions.
114 changes: 55 additions & 59 deletions leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,63 @@

// https://leetcode.com/problems/word-search/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordSearch {

// // TODO : fix V0
// // V0
// // IDEA : DFS OR BACKTRACK
// private int len;
// private int wid;
// private char[][] board_;
//
// public boolean exist(char[][] board, String word) {
//
// if (board == null || board.length == 0){
// return false;
// }
//
// len = board.length;
// wid = board[0].length;
// board_ = board;
//
// for (int y = 0; y < len; y++){
// for (int x = 0; x < wid; x++){
// int index = 0;
// if (this.backtrack_(x, y, word, index)){
// return true;
// }
// }
// }
//
// return false;
// }
//
// boolean backtrack_(int x, int y, String word, int index){
//
// if (index >= word.length()){
// return true;
// }
//
// if (x < 0 || x >= wid || y < 0 || y >= len || this.board_[y][x] != word.charAt(index)){
// return false;
// }
//
// //public int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
// int[][] moves = new int[][]{ {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
// for (int[] move : moves){
// index += 1;
// backtrack_(move[0], move[1], word, index);
// this.board[x][y] = word.charAt(index);
// index -= 1;
// }
// return false;
// }
// V0
// IDEA : DFS + BACKTRACK
public boolean exist(char[][] board, String word) {

int n = board.length;
int m = board[0].length;

/** NOTE !!! we define visited */
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(row, col, word, 0, visited, board)){
return true;
}
}
}
}
return false;
}

public boolean dfs(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;
}

/** NOTE !!! mark cur x, y as visited */
visited[row][col] = true;
if ( 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) ){
return true;
}

/** NOTE !!! Backtrack : undo cur x, y as visited */
visited[row][col] = false; // Backtrack

return false;
}

// V1
// IDEA : DFS + BACKTRACK
// https://leetcode.com/problems/word-search/solutions/4791515/java-easy-solution-dfs-backtracking/
public boolean exist(char[][] board, String word) {
public boolean exist2(char[][] board, String word) {
int n = board.length;
int m = board[0].length;

Expand All @@ -71,7 +67,7 @@ public boolean exist(char[][] board, String word) {
for(int row = 0; row < n; row++){
for(int col = 0; col < m; col++){
if(board[row][col] == word.charAt(0)){
if(dfs(row, col, word, 0, visited, board)){
if(dfs2(row, col, word, 0, visited, board)){
return true;
}
}
Expand All @@ -80,7 +76,7 @@ public boolean exist(char[][] board, String word) {
return false;
}

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

Expand All @@ -95,10 +91,10 @@ 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);
dfs2(row + 1, col, word, lvl + 1, visited, board) ||
dfs2(row - 1, col, word, lvl + 1, visited, board) ||
dfs2(row, col + 1, word, lvl + 1, visited, board) ||
dfs2(row, col - 1, word, lvl + 1, visited, board);

visited[row][col] = false; // Backtrack
return didFindNextCharacter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package LeetCodeJava.BackTrack;

// https://leetcode.com/problems/word-search-ii/description/?envType=list&envId=xoqag3yj

import java.util.List;

public class WordSearch2 {

// V0
public List<String> findWords(char[][] board, String[] words) {
return null;
}

}

0 comments on commit 9f55607

Please sign in to comment.