Skip to content

Commit

Permalink
update 79 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 4, 2024
1 parent cf057fa commit 4b1ad3a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 53 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240304: 73,76
20240304: 73,76,79(again)
20240303: 55(again),56,62,70
20240229: 39,48(again),49,53,54
20240228: 20,21,23,33(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
2024-04-28 -> ['73,76']
2024-04-28 -> ['73,76,79(again)']
2024-04-27 -> ['55(again),56,62,70']
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-07 -> ['73,76']
2024-04-07 -> ['73,76,79(again)']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-25 -> ['73,76']
2024-03-25 -> ['73,76,79(again)']
2024-03-24 -> ['55(again),56,62,70']
2024-03-21 -> ['39,48(again),49,53,54']
2024-03-20 -> ['20,21,23,33(again)']
2024-03-19 -> ['1,3,5,4,19']
2024-03-17 -> ['73,76']
2024-03-17 -> ['73,76,79(again)']
2024-03-16 -> ['55(again),56,62,70']
2024-03-13 -> ['39,48(again),49,53,54']
2024-03-12 -> ['73,76', '20,21,23,33(again)']
2024-03-12 -> ['73,76,79(again)', '20,21,23,33(again)']
2024-03-11 -> ['55(again),56,62,70', '1,3,5,4,19']
2024-03-09 -> ['73,76']
2024-03-09 -> ['73,76,79(again)']
2024-03-08 -> ['55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['73,76', '20,21,23,33(again)']
2024-03-06 -> ['73,76', '55(again),56,62,70', '1,3,5,4,19']
2024-03-05 -> ['73,76', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['73,76,79(again)', '20,21,23,33(again)']
2024-03-06 -> ['73,76,79(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-05 -> ['73,76,79(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-04 -> ['55(again),56,62,70', '20,21,23,33(again)']
2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19']
2024-03-02 -> ['39,48(again),49,53,54', '20,21,23,33(again)']
Expand Down
128 changes: 85 additions & 43 deletions leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,103 @@

public class WordSearch {

// TODO : fix V0
// // V0
// public boolean exist(char[][] board, String word) {
// // TODO : fix V0
// // V0
// // IDEA : DFS OR BACKTRACK
// private int len;
// private int wid;
// private char[][] board_;
//
// if (board == null || board.length == 0){
// return false;
// }
// public boolean exist(char[][] board, String word) {
//
// Boolean[][] visited = new Boolean[board.length][board[0].length];
// List<String> cur = new ArrayList<>();
// return _help(board, word, cur, visited);
// }
// if (board == null || board.length == 0){
// return false;
// }
//
// private boolean _help(char[][] board, String word, List<String> cur, Boolean[][] visited){
// len = board.length;
// wid = board[0].length;
// board_ = board;
//
// int len = board.length;
// int width = board[0].length;
// 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;
// }
// }
// }
//
// String curStr = array2String(cur);
// if (curStr.equals(word)){
// return true;
// }
// return false;
// }
//
// if (cur.size() > word.length()){
// return false;
// }
// boolean backtrack_(int x, int y, String word, int index){
//
// for (int i=0; i < len; i++){
// for (int j=0; j < width; j++){
// String val = String.valueOf(board[i][j]);
// if (visited[i][j] == null){
// // do recursive
// cur.add(val);
// visited[i][j] = true;
// _help(board, word, cur, visited);
// // undo
// visited[i][j] = null;
// cur.remove(cur.size()-1);
// _help(board, word, cur, visited);
// }
// }
// }
// if (index >= word.length()){
// return true;
// }
//
// return false;
// }
// if (x < 0 || x >= wid || y < 0 || y >= len || this.board_[y][x] != word.charAt(index)){
// return false;
// }
//
// private String array2String(List<String> input){
// String output = "";
// for (String x : input){
// output += x;
// }
// return output;
// //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;
// }


// V1
// IDEA : DFS
// https://leetcode.com/problems/word-search/solutions/4791515/java-easy-solution-dfs-backtracking/
public boolean exist(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(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;
}

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);

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

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

0 comments on commit 4b1ad3a

Please sign in to comment.