Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> tFreq = new HashMap<>();
for (char c : t.toCharArray()) {
tFreq.put(c, tFreq.getOrDefault(c, 0) + 1);
}

HashMap<Character, Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}