diff --git a/README.md b/README.md index 3b1e94fd..13be2440 100644 --- a/README.md +++ b/README.md @@ -989,7 +989,7 @@ 078| [Subsets](https://leetcode.com/problems/subsets/) | [Python](./leetcode_python/Backtracking/subsets.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BackTrack/Subsets.java) | _O(n * 2^n)_ | _O(1)_| Medium |`good concept` , similar as, dfs, back track, `#90 Subsets II `, `UBER`, `amazon`, `fb`| AGAIN**************** (7) (MUST again!!!) 090| [Subsets II](https://leetcode.com/problems/subsets-ii/) | [Python](./leetcode_python/Backtracking/subsets-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BackTrack/subsets2.java) | _O(n * 2^n)_ | _O(1)_ | Medium |good basic,check `# 078 Subsets`, dfs, backtrack, `fb`| AGAIN********* (6) 131| [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) |[Python](./leetcode_python/Backtracking/palindrome-partitioning.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BackTrack/PalindromePartitioning.java) | _O(n^2)_ ~ _O(2^n)_ | _O(n^2)_ | Medium |dfs, backtracking, good trick,`amazon`| AGAIN*********** (6) (MUST) -139| [Word Break](https://leetcode.com/problems/word-break/) | [Python](./leetcode_python/Backtracking/word-break.py) | _O(n * l^2)_ | _O(n)_ | Medium |Curated Top 75, backtracking, dfs, `dp`,`trick`,`UBER`,`yahoo`,`amazon`, `google`,`fb`| AGAIN**************** (7) (MUST) +139| [Word Break](https://leetcode.com/problems/word-break/) | [Python](./leetcode_python/Backtracking/word-break.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java) | _O(n * l^2)_ | _O(n)_ | Medium |Curated Top 75, backtracking, dfs, `dp`,`trick`,`UBER`,`yahoo`,`amazon`, `google`,`fb`| AGAIN**************** (7) (MUST) 140| [Word Break II](https://leetcode.com/problems/word-break-ii/) | [Python](./leetcode_python/Backtracking/word-break-ii.py) | _O(n * l^2)_ | _O(n)_ | Hard |good basic, LC 078, backtracking, dfs, `dp`,`trick`, `amazon`| AGAIN******** (2) 212| [Word Search II](https://leetcode.com/problems/word-search-ii/) | [Python](./leetcode_python/Backtracking/word-search-ii.py) | _O(m * n * l)_ | _O(l)_ | Hard |Curated Top 75, LC 208, LC 079, hashset, hashmap, trie, backtrack with trie, `amazon`, fb, apple, twitter, uber, google, indeed| AGAIN********** (3) 216| [Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Python](./leetcode_python/Backtracking/combination-sum-iii.py) | _O(k * C(n, k))_ | _O(k)_ | Medium || AGAIN* diff --git a/data/progress.txt b/data/progress.txt index 2ebe25bf..8445ddaa 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240307: 139 20240306: 121,124(again),125,128,133(again) 20240305: 100,102,104,105(again) 20240304: 73,76,79(again),91,25 diff --git a/data/to_review.txt b/data/to_review.txt index b15251f2..6033387a 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,3 +1,4 @@ +2024-05-01 -> ['139'] 2024-04-30 -> ['121,124(again),125,128,133(again)'] 2024-04-29 -> ['100,102,104,105(again)'] 2024-04-28 -> ['73,76,79(again),91,25'] @@ -5,6 +6,7 @@ 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-10 -> ['139'] 2024-04-09 -> ['121,124(again),125,128,133(again)'] 2024-04-08 -> ['100,102,104,105(again)'] 2024-04-07 -> ['73,76,79(again),91,25'] @@ -12,23 +14,25 @@ 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-28 -> ['139'] 2024-03-27 -> ['121,124(again),125,128,133(again)'] 2024-03-26 -> ['100,102,104,105(again)'] 2024-03-25 -> ['73,76,79(again),91,25'] 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-20 -> ['139', '20,21,23,33(again)'] 2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19'] 2024-03-18 -> ['100,102,104,105(again)'] 2024-03-17 -> ['73,76,79(again),91,25'] 2024-03-16 -> ['55(again),56,62,70'] +2024-03-15 -> ['139'] 2024-03-14 -> ['121,124(again),125,128,133(again)'] 2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54'] -2024-03-12 -> ['73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-12 -> ['139', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-11 -> ['121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] -2024-03-10 -> ['100,102,104,105(again)'] -2024-03-09 -> ['121,124(again),125,128,133(again)', '73,76,79(again),91,25'] -2024-03-08 -> ['121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-10 -> ['139', '100,102,104,105(again)'] +2024-03-09 -> ['139', '121,124(again),125,128,133(again)', '73,76,79(again),91,25'] +2024-03-08 -> ['139', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] 2024-03-07 -> ['121,124(again),125,128,133(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java new file mode 100644 index 00000000..18795f06 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java @@ -0,0 +1,148 @@ +package LeetCodeJava.BackTrack; + +// https://leetcode.com/problems/word-break/description/ + +import java.util.*; + +public class WordBreak { + + // V0 + // IDEA : BACKTRACK + // TODO: fix below +// public boolean wordBreak(String s, List wordDict) { +// String cur = ""; +// return this.check(s, wordDict, cur, 0); +// } +// +// private Boolean check(String s, List wordDict, String cur, int idx){ +// if (cur == s){ +// return true; +// } +// if (cur.length() > s.length()){ +// return false; +// } +// for (int i = idx; i < wordDict.size(); i++){ +// Integer curLen = cur.length(); +// String word = wordDict.get(i); +// cur += word; +// this.check(s, wordDict, cur, i); +// // undo +// cur = cur.substring(0, curLen-1); +// } +// return false; +// } + + // V0 + // IDEA : BACKTRACK (modify above via GPT) + public boolean wordBreak_(String s, List wordDict) { + return check(s, new HashSet<>(wordDict), 0, new Boolean[s.length()]); + } + + private boolean check(String s, Set wordSet, int start, Boolean[] memo) { + if (start == s.length()) { + return true; + } + if (memo[start] != null) { + return memo[start]; + } + + for (int end = start + 1; end <= s.length(); end++) { + if (wordSet.contains(s.substring(start, end)) && check(s, wordSet, end, memo)) { + return memo[start] = true; + } + } + return memo[start] = false; + } + + + // V0'' + // IDEA : BACKTRACK + Boolean[] memo_; + + public boolean wordBreak_0(String s, List wordDict) { + memo_ = new Boolean[s.length()]; + return help(s, wordDict, 0); + } + + private boolean help(String s, List wordDict, int start) { + if (start == s.length()) { + return true; + } + if (memo_[start] != null) { + return memo_[start]; + } + for (int end = start + 1; end <= s.length(); end++) { + if (wordDict.contains(s.substring(start, end)) && help(s, wordDict, end)) { + memo_[start] = true; + return true; + } + } + memo_[start] = false; + return false; + } + + // V1 + // IDEA : DFS + // https://leetcode.com/problems/word-break/ + public boolean wordBreak_1(String s, List wordDict) { + Map memo = new HashMap<>(); + Set wordSet = new HashSet<>(wordDict); + return dfs(s, wordSet, memo); + } + + private boolean dfs(String s, Set wordSet, Map memo) { + if (memo.containsKey(s)) return memo.get(s); + if (wordSet.contains(s)) return true; + for (int i = 1; i < s.length(); i++) { + String prefix = s.substring(0, i); + if (wordSet.contains(prefix) && dfs(s.substring(i), wordSet, memo)) { + memo.put(s, true); + return true; + } + } + memo.put(s, false); + return false; + } + + // V2 + // IDEA : MEMORIZATION + // https://leetcode.com/problems/word-break/solutions/3687504/recursion-memoization-tabulation/ + Boolean[] memo; + public boolean wordBreak_2(String s, List wordDict) { + memo=new Boolean[s.length()]; + return _wordBreak(s,wordDict,0); + } + public boolean _wordBreak(String s, List wordDict, int si) { + if(si>=s.length()){ + return true; + } + if(memo[si]!=null) return memo[si]; + for(int i=si; i wordDict) { + boolean[] dp=new boolean[s.length()+1]; + dp[dp.length-1]=true; + for(int i=dp.length-2; i>=0; i--){ + for(int j=i; j