Skip to content

Commit

Permalink
add 76 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 4, 2024
1 parent c9e6252 commit cf057fa
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@
043| [Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [Python](./leetcode_python/String/multiply-strings.py) | _O(m * n)_ | _O(m + n)_ | Medium |`twitter`, `fb`| AGAIN*** (3)
058| [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [Python](./leetcode_python/String/length-of-last-word.py) | _O(n)_ | _O(1)_ | Easy || OK
067| [Add Binary](https://leetcode.com/problems/add-binary/) | [Python](./leetcode_python/String/add-binary.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/AddBinary.java) | _O(n)_ | _O(1)_ | Easy | bit op, `good basic`, `fb`, `amazon`| OK***** (7) (but again)
076| [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Python](./leetcode_python/String/minimum_window_substring.py) | | | Hard |`Curated Top 75` | AGAIN******* (2) (not start)
076| [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [Python](./leetcode_python/String/minimum_window_substring.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/String/MinimumWindowSubstring.java) | | | Hard |`Curated Top 75` | AGAIN******* (2) (not start)
125| [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](./leetcode_python/String/valid-palindrome.py) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, LC 680, `amazon`,`fb`| OK
151| [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Python](./leetcode_python/String/reverse-words-in-a-string.py) | _O(n)_ | _O(1)_ | Medium |`amazon`,`fb`| OK (2)
161| [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) | [Python](./leetcode_python/String/one-edit-distance.py) | _O(m + n)_ | _O(1)_ | Medium |🔒, trick, recursion, string, , `fb`| AGAIN ******** (6)(AGAIN)
Expand Down
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
20240304: 73,76
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']
2024-04-28 -> ['73,76']
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']
2024-04-07 -> ['73,76']
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']
2024-03-25 -> ['73,76']
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']
2024-03-17 -> ['73,76']
2024-03-16 -> ['55(again),56,62,70']
2024-03-13 -> ['39,48(again),49,53,54']
2024-03-12 -> ['73', '20,21,23,33(again)']
2024-03-12 -> ['73,76', '20,21,23,33(again)']
2024-03-11 -> ['55(again),56,62,70', '1,3,5,4,19']
2024-03-09 -> ['73']
2024-03-09 -> ['73,76']
2024-03-08 -> ['55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['73', '20,21,23,33(again)']
2024-03-06 -> ['73', '55(again),56,62,70', '1,3,5,4,19']
2024-03-05 -> ['73', '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-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
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package LeetCodeJava.String;

// https://leetcode.com/problems/minimum-window-substring/description/?envType=list&envId=xoqag3yj

import java.util.HashMap;
import java.util.Map;


public class MinimumWindowSubstring {

// V0
// TODO : implement
public String minWindow(String s, String t) {

return null;
}

// V1
// IDEA: SLIDING WINDOW
// https://leetcode.com/problems/minimum-window-substring/solutions/4674237/easy-explanation-solution/?envType=list&envId=xoqag3yj
public String minWindow_1(String s, String t) {
Map<Character, Integer> targetFreq = new HashMap<>();
Map<Character, Integer> windowFreq = new HashMap<>();

for (char c : t.toCharArray()) {
targetFreq.put(c, targetFreq.getOrDefault(c, 0) + 1);
}

int left = 0, right = 0, minLength = Integer.MAX_VALUE, minLeft = 0, requiredChars = targetFreq.size();

while (right < s.length()) {
char currentChar = s.charAt(right);

windowFreq.put(currentChar, windowFreq.getOrDefault(currentChar, 0) + 1);

if (targetFreq.containsKey(currentChar) && windowFreq.get(currentChar).equals(targetFreq.get(currentChar))) {
requiredChars--;
}

while (requiredChars == 0) {
if (right - left + 1 < minLength) {
minLength = right - left + 1;
minLeft = left;
}

char leftChar = s.charAt(left);
windowFreq.put(leftChar, windowFreq.get(leftChar) - 1);

if (targetFreq.containsKey(leftChar) && windowFreq.get(leftChar) < targetFreq.get(leftChar)) {
requiredChars++;
}

left++;
}

right++;
}

if (minLength == Integer.MAX_VALUE) {
return "";
}

return s.substring(minLeft, minLeft + minLength);
}

// V2
// https://leetcode.com/problems/minimum-window-substring/solutions/4673541/beats-100-explained-any-language-by-prodonik/?envType=list&envId=xoqag3yj
public String minWindow_2(String s, String t) {
if (s == null || t == null || s.length() == 0 || t.length() == 0 ||
s.length() < t.length()) {
return new String();
}
int[] map = new int[128];
int count = t.length();
int start = 0, end = 0, minLen = Integer.MAX_VALUE, startIndex = 0;
/// UPVOTE !
for (char c : t.toCharArray()) {
map[c]++;
}

char[] chS = s.toCharArray();

while (end < chS.length) {
if (map[chS[end++]]-- > 0) {
count--;
}
while (count == 0) {
if (end - start < minLen) {
startIndex = start;
minLen = end - start;
}
if (map[chS[start++]]++ == 0) {
count++;
}
}
}

return minLen == Integer.MAX_VALUE ? new String() :
new String(chS, startIndex, minLen);
}

}

0 comments on commit cf057fa

Please sign in to comment.