Skip to content

Commit

Permalink
add 139 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 7, 2024
1 parent 1f1e7ff commit f04b574
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 9 additions & 5 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
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']
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-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']
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-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']
Expand Down
148 changes: 148 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java
Original file line number Diff line number Diff line change
@@ -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<String> wordDict) {
// String cur = "";
// return this.check(s, wordDict, cur, 0);
// }
//
// private Boolean check(String s, List<String> 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<String> wordDict) {
return check(s, new HashSet<>(wordDict), 0, new Boolean[s.length()]);
}

private boolean check(String s, Set<String> 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<String> wordDict) {
memo_ = new Boolean[s.length()];
return help(s, wordDict, 0);
}

private boolean help(String s, List<String> 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<String> wordDict) {
Map<String, Boolean> memo = new HashMap<>();
Set<String> wordSet = new HashSet<>(wordDict);
return dfs(s, wordSet, memo);
}

private boolean dfs(String s, Set<String> wordSet, Map<String, Boolean> 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<String> wordDict) {
memo=new Boolean[s.length()];
return _wordBreak(s,wordDict,0);
}
public boolean _wordBreak(String s, List<String> wordDict, int si) {
if(si>=s.length()){
return true;
}
if(memo[si]!=null) return memo[si];
for(int i=si; i<s.length(); i++){
if(wordDict.contains(s.substring(si,i+1))){
if(_wordBreak(s,wordDict,i+1)){
return memo[si] = true;
}
}
}
return memo[si] = false;
}

// V3
// IDEA : Tabulation
// https://leetcode.com/problems/word-break/solutions/3687504/recursion-memoization-tabulation/
public boolean wordBreak_3(String s, List<String> 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<s.length() ; j++){
if(wordDict.contains(s.substring(i,j+1))){
if(dp[j+1]){
dp[i] = true;
}
}
}
}
return dp[0];
}

}

0 comments on commit f04b574

Please sign in to comment.