-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Add Minimum Window Substring algorithm in Sliding Window package #6553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 16 commits into
TheAlgorithms:master
from
Anubhav-pandey004:master
Oct 2, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6d8d82e
Create MinimumWindowSubstring.java
Anubhav-pandey004 334d6a8
Create MinimumWindowSubstringTest.java
Anubhav-pandey004 3a553e8
correct MinimumWindowSubstringTest.java
Anubhav-pandey004 afcbf77
Merge branch 'master' into master
Anubhav-pandey004 6a678a0
Merge branch 'master' into master
DenizAltunkapan de3e1d7
Added more test cases
Anubhav-pandey004 a1f5e40
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 e3ceeb2
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 daf3579
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 cb2838f
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 455a6ed
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 b2bf70b
Update MinimumWindowSubstringTest.java
Anubhav-pandey004 b106dc3
Merge branch 'master' into master
Anubhav-pandey004 5fb086b
Added test case "AAABBC" "AABC"
Anubhav-pandey004 5b633f0
Merge branch 'master' into master
Anubhav-pandey004 235d2fa
Merge branch 'master' into master
DenizAltunkapan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/com/thealgorithms/slidingwindow/MinimumWindowSubstring.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/com/thealgorithms/slidingwindow/MinimumWindowSubstringTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.