From 5c1fa63a741a93a7559ce57ebbbfb2f49a968990 Mon Sep 17 00:00:00 2001 From: Anubhav <110095813+Anubhav-pandey004@users.noreply.github.com> Date: Mon, 29 Sep 2025 11:00:07 +0530 Subject: [PATCH 1/5] Create MinimumWindowSubstring leetcode 76. Minimum Window Substring --- .../slidingwindow/MinimumWindowSubstring | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring new file mode 100644 index 000000000000..423b8b6a497a --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring @@ -0,0 +1,55 @@ +package com.thealgorithms.slidingwindow; + +/** + * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length + * of the longest subarray whose sum is less than or equal to a given value k. + * + *

+ * Worst-case performance O(n) + * Best-case performance O(n) + * Average performance O(n) + * Worst-case space complexity O(1) + * + * @author https://github.com/Chiefpatwal + */ +class Solution { + public static String minWindow(String s, String t) { + if (s.length() < t.length()) return ""; + + HashMap tFreq = new HashMap<>(); + for (char c : t.toCharArray()) { + tFreq.put(c, tFreq.getOrDefault(c, 0) + 1); + } + + HashMap windowFreq = new HashMap<>(); + int left = 0, right = 0, minLen = Integer.MAX_VALUE, count = 0; + String result = ""; + + while (right < s.length()) { + char c = s.charAt(right); + windowFreq.put(c, windowFreq.getOrDefault(c, 0) + 1); + + if (tFreq.containsKey(c) && windowFreq.get(c).intValue() <= tFreq.get(c).intValue()) { + count++; + } + + while (count == t.length()) { + if (right - left + 1 < minLen) { + minLen = right - left + 1; + result = s.substring(left, right + 1); + } + + char leftChar = s.charAt(left); + windowFreq.put(leftChar, windowFreq.get(leftChar) - 1); + + if (tFreq.containsKey(leftChar) && windowFreq.get(leftChar) < tFreq.get(leftChar)) { + count--; + } + + left++; + } + right++; + } + return result; + } +} From 504078c052463f69cd3d98f33724b193551553dd Mon Sep 17 00:00:00 2001 From: Anubhav <110095813+Anubhav-pandey004@users.noreply.github.com> Date: Mon, 29 Sep 2025 15:06:06 +0530 Subject: [PATCH 2/5] Update and rename MinimumWindowSubstring to MinimumWindowSubstring.java --- ...indowSubstring => MinimumWindowSubstring.java} | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) rename src/main/java/com/thealgorithms/slidingwindow/{MinimumWindowSubstring => MinimumWindowSubstring.java} (80%) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java similarity index 80% rename from src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring rename to src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java index 423b8b6a497a..38b00ffcbf8d 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java @@ -1,5 +1,5 @@ package com.thealgorithms.slidingwindow; - +import java.util.*; /** * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length * of the longest subarray whose sum is less than or equal to a given value k. @@ -12,7 +12,18 @@ * * @author https://github.com/Chiefpatwal */ -class Solution { +public final class MinimumWindowSubstring { + // Prevent instantiation + private MinimumWindowSubstring() { + } + + /** + * This method finds the minimum sum of a subarray of a given size k. + * + * @param arr is the input array where the minimum sum needs to be found + * @param k is the size of the subarray + * @return the minimum sum of the subarray of size k + */ public static String minWindow(String s, String t) { if (s.length() < t.length()) return ""; From 1d2d6d7891fc4cc5873816226eec922f95a0d44a Mon Sep 17 00:00:00 2001 From: Anubhav <110095813+Anubhav-pandey004@users.noreply.github.com> Date: Mon, 29 Sep 2025 15:17:41 +0530 Subject: [PATCH 3/5] Update MinimumWindowSubstring.java --- .../com/thealgorithms/slidingwindow/MinimumWindowSubstring.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java index 38b00ffcbf8d..ca8042ee88b2 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java @@ -1,5 +1,5 @@ package com.thealgorithms.slidingwindow; -import java.util.*; +import java.util.HashMap; /** * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length * of the longest subarray whose sum is less than or equal to a given value k. From 27d6f0b4606681b18da8c0a15c43198ead072423 Mon Sep 17 00:00:00 2001 From: Anubhav <110095813+Anubhav-pandey004@users.noreply.github.com> Date: Mon, 29 Sep 2025 15:32:16 +0530 Subject: [PATCH 4/5] Update MinimumWindowSubstring.java --- .../slidingwindow/MinimumWindowSubstring.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java index ca8042ee88b2..3e5321a6c547 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java @@ -25,7 +25,10 @@ private MinimumWindowSubstring() { * @return the minimum sum of the subarray of size k */ public static String minWindow(String s, String t) { - if (s.length() < t.length()) return ""; + if (s.length() < t.length()) + { + return ""; + } HashMap tFreq = new HashMap<>(); for (char c : t.toCharArray()) { @@ -33,7 +36,10 @@ public static String minWindow(String s, String t) { } HashMap windowFreq = new HashMap<>(); - int left = 0, right = 0, minLen = Integer.MAX_VALUE, count = 0; + int left = 0; + int right = 0; + int minLen = Integer.MAX_VALUE; + int count = 0; String result = ""; while (right < s.length()) { @@ -43,7 +49,6 @@ public static String minWindow(String s, String t) { if (tFreq.containsKey(c) && windowFreq.get(c).intValue() <= tFreq.get(c).intValue()) { count++; } - while (count == t.length()) { if (right - left + 1 < minLen) { minLen = right - left + 1; @@ -52,7 +57,6 @@ public static String minWindow(String s, String t) { char leftChar = s.charAt(left); windowFreq.put(leftChar, windowFreq.get(leftChar) - 1); - if (tFreq.containsKey(leftChar) && windowFreq.get(leftChar) < tFreq.get(leftChar)) { count--; } From 6b60f2f1911e00fa715437d17fbf2b1022f73066 Mon Sep 17 00:00:00 2001 From: Anubhav <110095813+Anubhav-pandey004@users.noreply.github.com> Date: Mon, 29 Sep 2025 15:39:31 +0530 Subject: [PATCH 5/5] Update MinimumWindowSubstring.java --- .../slidingwindow/MinimumWindowSubstring.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java index 3e5321a6c547..c1a5ac067ab5 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java @@ -1,10 +1,10 @@ package com.thealgorithms.slidingwindow; + import java.util.HashMap; + /** - * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length - * of the longest subarray whose sum is less than or equal to a given value k. + * Finds the minimum window substring in 's' that contains all characters of 't'. * - *

* Worst-case performance O(n) * Best-case performance O(n) * Average performance O(n) @@ -18,15 +18,14 @@ private MinimumWindowSubstring() { } /** - * This method finds the minimum sum of a subarray of a given size k. + * Finds the minimum window substring of 's' containing all characters of 't'. * - * @param arr is the input array where the minimum sum needs to be found - * @param k is the size of the subarray - * @return the minimum sum of the subarray of size k + * @param s The input string to search within. + * @param t The string with required characters. + * @return The minimum window substring, or empty string if not found. */ - public static String minWindow(String s, String t) { - if (s.length() < t.length()) - { + public static String minWindow(String s, String t) { + if (s.length() < t.length()) { return ""; } @@ -37,7 +36,7 @@ public static String minWindow(String s, String t) { HashMap windowFreq = new HashMap<>(); int left = 0; - int right = 0; + int right = 0; int minLen = Integer.MAX_VALUE; int count = 0; String result = ""; @@ -49,18 +48,18 @@ public static String minWindow(String s, String t) { if (tFreq.containsKey(c) && windowFreq.get(c).intValue() <= tFreq.get(c).intValue()) { count++; } + while (count == t.length()) { if (right - left + 1 < minLen) { minLen = right - left + 1; result = s.substring(left, right + 1); } - + char leftChar = s.charAt(left); windowFreq.put(leftChar, windowFreq.get(leftChar) - 1); if (tFreq.containsKey(leftChar) && windowFreq.get(leftChar) < tFreq.get(leftChar)) { count--; } - left++; } right++;