diff --git a/README.md b/README.md index f04bd3dd..7408df36 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 9db3cb35..7183c398 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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) diff --git a/data/to_review.txt b/data/to_review.txt index d8db8739..b3442327 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/String/MinimumWindowSubstring.java b/leetcode_java/src/main/java/LeetCodeJava/String/MinimumWindowSubstring.java new file mode 100644 index 00000000..5e79faa8 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/String/MinimumWindowSubstring.java @@ -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 targetFreq = new HashMap<>(); + Map 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); + } + +}