diff --git a/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java new file mode 100644 index 000000000000..c1a5ac067ab5 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java @@ -0,0 +1,69 @@ +package com.thealgorithms.slidingwindow; + +import java.util.HashMap; + +/** + * 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) + * Worst-case space complexity O(1) + * + * @author https://github.com/Chiefpatwal + */ +public final class MinimumWindowSubstring { + // Prevent instantiation + private MinimumWindowSubstring() { + } + + /** + * Finds the minimum window substring of 's' containing all characters of 't'. + * + * @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()) { + 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; + int right = 0; + int minLen = Integer.MAX_VALUE; + int 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; + } +} diff --git a/src/test/java/com/thealgorithms/slidingwindow/MinimumWindowSubstringTest.java b/src/test/java/com/thealgorithms/slidingwindow/MinimumWindowSubstringTest.java new file mode 100644 index 000000000000..2c1534f4bfe6 --- /dev/null +++ b/src/test/java/com/thealgorithms/slidingwindow/MinimumWindowSubstringTest.java @@ -0,0 +1,35 @@ +package com.thealgorithms.slidingwindow; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +/** + * Finds the minimum window substring in {@code s} that contains all characters of {@code t}. + * + * @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 + * @author (https://github.com/Chiefpatwal) + */ +public class MinimumWindowSubstringTest { + + /** + * Tests for MinimumWindowSubstring.minWindow. + */ + @Test + public void testMinimumWindowSubstring() { + assertEquals("BANC", MinimumWindowSubstring.minWindow("ADOBECODEBANC", "ABC")); + assertEquals("a", MinimumWindowSubstring.minWindow("a", "a")); + assertEquals("", MinimumWindowSubstring.minWindow("a", "aa")); + assertEquals("", MinimumWindowSubstring.minWindow("ADOBECODEBANC", "XYZ")); + assertEquals("BC", MinimumWindowSubstring.minWindow("ABCDEF", "BC")); + assertEquals("q", MinimumWindowSubstring.minWindow("abcdefghijklmnopqrstuvwxyz", "q")); + assertEquals("", MinimumWindowSubstring.minWindow("zzzzzzzzz", "zzzzzzzzzz")); + assertEquals("abbbbbcdd", MinimumWindowSubstring.minWindow("aaaaaaaaaaaabbbbbcdd", "abcdd")); + assertEquals("ABCDEFG", MinimumWindowSubstring.minWindow("ABCDEFG", "ABCDEFG")); + assertEquals("", MinimumWindowSubstring.minWindow("abc", "A")); + assertEquals("A", MinimumWindowSubstring.minWindow("aAbBcC", "A")); + assertEquals("AABBC", MinimumWindowSubstring.minWindow("AAABBC", "AABC")); + } +}