76. Minimum Window Substring
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 test cases 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.length
n == t.length
1 <= m, n <= 105
s
andt
consist of uppercase and lowercase English letters.
Follow up: Could you find an algorithm that runs in O(m + n)
time?
- From the name of the problem, you see a "window" in it.
- So basically it's trying to give us a hint, that is, we should find some "window" that contains all the characters in
t
and it has the shortest length. - So, sliding window algorithm.
- Sliding window is a very useful technique that involves a left and right pointer, and by moving the left and right pointers, we can find each possible window that satisfies our constraint that it should contain all the characters in
t
. - So how do we know if a window contains all characters in
t
? - We can definitely use a counter and hash map that records how many times each character appears in
t
. - For example, for
t = aabc
, we knowfrequencies = { a: 2, b: 1, c: 1 }
andcount = 4
. It means we need to find a substring ins
that contains 4 characters that are 2a
s, 1b
and 1c
. - Initialize
l = 0, r = 0, count = 0
.l
is the left pointer,r
is the right pointer andcount
is how many character we collected fort
so far. By moving the right pointerr
, when we meet one character oft
ins
, we can incrementcount
and frequencies of each character. But we only incrementcount
of the character if its frequency is still greater than 0. - For example, at a certain point, we have
{ a: 2 }
. It means, to cover the charactera
int
, we still need the substring ins
to contain 2a
s. However, once it becomes 0, we can still decrement its frequency in the map, but we cannot incrementcount
, because it does not count as one character that we must cover. - But what does the negative frequency in the map mean? It means that we covered more
a
s than necessary in the substring ins
fort
. - So when
count == t.length
, we know that we covered all characters int
. We can find the start index and its length. - Also we will try moving
l
forward to squeeze it, and we might find an even smaller window. - Notice that we only decrement
count
for character, for example,a
, when frequency ofa
becomes greater than or equal to 0. It means, when you movel
forward, you'll miss onea
, so one less character in t wecount
ed so far. - Think of the
frequency map
andcount
are some kind of state. Whencount == t.length
, we know we find a window that can cover every character int
, but it could be smaller if the character at the left end is not amust-include
.
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
const minWindow = function (s, t) {
if (!s || !t || s.length < t.length) return "";
let l = 0, r = 0;
let count = 0, minI = s.length + 1, minL = s.length + 1;
const freqMap = {};
for (const ch of t) freqMap[ch] = (freqMap[ch] || 0) + 1;
while (r < s.length) {
if (freqMap[s[r]]-- >= 1) count += 1;
r += 1;
while (count == t.length) {
if (r - l < minL) {
minL = r - l;
minI = l;
}
if (freqMap[s[l]]++ >= 0) count -= 1;
l += 1;
}
}
return s.slice(minI, minI + minL);
};
That’s all folks! In this post, we solved LeetCode problem #76. Minimum Window Substring
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
In this blog, I have tried to solve leetcode questions & present the most important points to consider when improving Data structure and logic, feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!
Comments
Post a Comment