Class Solution
Hard
Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t ( including duplicates ) is included in the window. If there is no such substring_, return the empty string_ "".
The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = “ADOBECODEBANC”, t = “ABC”
Output: “BANC”
Explanation: The minimum window substring “BANC” includes ‘A’, ‘B’, and ‘C’ from string t.
Example 2:
Input: s = “a”, t = “a”
Output: “a”
Explanation: The entire string s is the minimum window.
Example 3:
Input: s = “a”, t = “aa”
Output: ""
Explanation: Both ’a’s from t must be included in the window. Since the largest window of s only has one ‘a’, return empty string.
Constraints:
m == s.lengthn == t.length1 <= m, n <= 105sandtconsist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in O(m + n) time?
To solve the “Minimum Window Substring” problem in Java with the Solution class, follow these steps:
- Define a method
minWindowin theSolutionclass that takes two stringssandtas input and returns the minimum window substring ofscontaining all characters fromt. - Create two frequency maps:
tFreqMapto store the frequency of characters in stringt, andsFreqMapto store the frequency of characters in the current window of strings. - Initialize two pointers
leftandrightto track the window boundaries. Initialize a variableminLengthto store the minimum window length found so far. - Iterate over string
susing therightpointer until the end of the string:- Update the frequency map
sFreqMapfor the character at indexright. - Check if the current window contains all characters from
t. If it does, move theleftpointer to minimize the window while maintaining the condition. - Update the
minLengthif the current window length is smaller. - Move the
rightpointer to expand the window.
- Update the frequency map
- Return the minimum window substring found, or an empty string if no such substring exists.
Here’s the implementation of the minWindow method in Java:
import java.util.HashMap;
import java.util.Map;
class Solution {
public String minWindow(String s, String t) {
Map<Character, Integer> tFreqMap = new HashMap<>();
Map<Character, Integer> sFreqMap = new HashMap<>();
// Initialize tFreqMap with character frequencies from string t
for (char ch : t.toCharArray()) {
tFreqMap.put(ch, tFreqMap.getOrDefault(ch, 0) + 1);
}
int left = 0;
int right = 0;
int minLength = Integer.MAX_VALUE;
int minStart = 0;
while (right < s.length()) {
char rightChar = s.charAt(right);
sFreqMap.put(rightChar, sFreqMap.getOrDefault(rightChar, 0) + 1);
right++;
// Check if the current window contains all characters from t
while (containsAllChars(sFreqMap, tFreqMap)) {
// Update the minimum window length
if (right - left < minLength) {
minLength = right - left;
minStart = left;
}
char leftChar = s.charAt(left);
sFreqMap.put(leftChar, sFreqMap.get(leftChar) - 1);
left++;
}
}
return minLength == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLength);
}
// Helper method to check if sFreqMap contains all characters from tFreqMap
private boolean containsAllChars(Map<Character, Integer> sFreqMap, Map<Character, Integer> tFreqMap) {
for (Map.Entry<Character, Integer> entry : tFreqMap.entrySet()) {
char ch = entry.getKey();
int count = entry.getValue();
if (sFreqMap.getOrDefault(ch, 0) < count) {
return false;
}
}
return true;
}
}
This implementation finds the minimum window substring in O(m + n) time complexity, where m is the length of string s and n is the length of string t. It uses two frequency maps to keep track of character frequencies and adjusts the window boundaries to find the minimum window containing all characters from t.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minWindow
-