Minimum Window Substring
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.
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.
Solution
Section titled “Solution”Explanation
Section titled “Explanation”The idea is to use a sliding window approach with two pointers, l and r, representing the left and right boundaries of the window. We maintain a hash map countT to store the frequency of characters in t and another hash map window to store the frequency of characters in the current window of s. We also keep track of how many characters we have matched with have and how many we need with need.
We expand the window by moving the right pointer r and update the window hash map. When the window contains all characters of t (i.e., have === need), we try to shrink the window from the left by moving the left pointer l while maintaining the condition. We update the result whenever we find a smaller valid window.
Implementation
Section titled “Implementation”function minWindow(s, t) { const countT = {}; for (const ch of t) { countT[ch] = (countT[ch] || 0) + 1; }
const window = {}; let have = 0, need = Object.keys(countT).length; let l = 0, res = [0, -1], resLen = Infinity;
for (let r = 0; r < s.length; r++) { const c = s[r]; window[c] = (window[c] || 0) + 1;
if (c in countT && window[c] === countT[c]) { have += 1; }
while (have === need) { if (r - l + 1 < resLen) { resLen = r - l + 1; res = [l, r]; }
const c0 = s[l]; window[c0] -= 1; if (c0 in countT && window[c0] < countT[c0]) { have -= 1; } l += 1; } }
const [start, end] = res; return s.slice(start, end + 1);}from collections import defaultdict
def minWindow(self, s: str, t: str) -> str: window, countT = defaultdict(int), defaultdict(int)
for ch in t: countT[ch] += 1
have, need = 0, len(countT) l = 0 res, resLen = [0, -1], float('inf')
for r in range(len(s)): c = s[r] window[c] += 1
if c in countT and window[c] == countT[c]: have += 1
while have == need: if r-l+1 < resLen: resLen = r-l+1 res = [l, r]
c0 = s[l] window[c0] -= 1
if c0 in countT and window[c0] < countT[c0]: have -= 1 l += 1
l, r = res return s[l:r+1]Complexity and Features
Section titled “Complexity and Features”- Time Complexity:
O(m + n)because we are iterating through both stringssandt. - Space Complexity:
O(m + n)as we are using hash maps to store character counts. - Features: Efficiently finds the minimum window substring containing all characters of
tins.