diff --git a/data/progress.txt b/data/progress.txt index 7183c398..e495035e 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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) diff --git a/data/to_review.txt b/data/to_review.txt index b3442327..6693280e 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java index 1cffb217..537e70ea 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordSearch.java @@ -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 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 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 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;