11package com .thealgorithms .slidingwindow ;
2+
23import java .util .HashMap ;
4+
35/**
4- * The Longest Subarray with Sum Less Than or Equal to k algorithm finds the length
5- * of the longest subarray whose sum is less than or equal to a given value k.
6+ * Finds the minimum window substring in 's' that contains all characters of 't'.
67 *
7- * <p>
88 * Worst-case performance O(n)
99 * Best-case performance O(n)
1010 * Average performance O(n)
@@ -18,15 +18,14 @@ private MinimumWindowSubstring() {
1818 }
1919
2020 /**
21- * This method finds the minimum sum of a subarray of a given size k .
21+ * Finds the minimum window substring of 's' containing all characters of 't' .
2222 *
23- * @param arr is the input array where the minimum sum needs to be found
24- * @param k is the size of the subarray
25- * @return the minimum sum of the subarray of size k
23+ * @param s The input string to search within.
24+ * @param t The string with required characters.
25+ * @return The minimum window substring, or empty string if not found.
2626 */
27- public static String minWindow (String s , String t ) {
28- if (s .length () < t .length ())
29- {
27+ public static String minWindow (String s , String t ) {
28+ if (s .length () < t .length ()) {
3029 return "" ;
3130 }
3231
@@ -37,7 +36,7 @@ public static String minWindow(String s, String t) {
3736
3837 HashMap <Character , Integer > windowFreq = new HashMap <>();
3938 int left = 0 ;
40- int right = 0 ;
39+ int right = 0 ;
4140 int minLen = Integer .MAX_VALUE ;
4241 int count = 0 ;
4342 String result = "" ;
@@ -49,18 +48,18 @@ public static String minWindow(String s, String t) {
4948 if (tFreq .containsKey (c ) && windowFreq .get (c ).intValue () <= tFreq .get (c ).intValue ()) {
5049 count ++;
5150 }
51+
5252 while (count == t .length ()) {
5353 if (right - left + 1 < minLen ) {
5454 minLen = right - left + 1 ;
5555 result = s .substring (left , right + 1 );
5656 }
57-
57+
5858 char leftChar = s .charAt (left );
5959 windowFreq .put (leftChar , windowFreq .get (leftChar ) - 1 );
6060 if (tFreq .containsKey (leftChar ) && windowFreq .get (leftChar ) < tFreq .get (leftChar )) {
6161 count --;
6262 }
63-
6463 left ++;
6564 }
6665 right ++;
0 commit comments