Given two strings s and t of lengths m and n respectively, return the minimum windowsubstring 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.
Pseudocode
Solution
// from solutions
// https://leetcode.com/problems/minimum-window-substring/solutions/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems/
var minWindow = function (s, t) {
let counter = t.length;
let shortestSubstring = "";
const tMap = {};
let start = 0;
let end = 0;
let minStart = 0;
let minLen = Infinity;
let size = s.length;
for (let i = 0; i < t.length; i++) {
if (!tMap[t[i]]) {
tMap[t[i]] = 0;
}
tMap[t[i]] += 1;
}
while (end < size) {
if (tMap[s[end]] > 0) {
counter--;
}
tMap[s[end]]--;
end++;
console.log(tMap);
while (counter === 0) {
if (end - start < minLen) {
minStart = start;
minLen = end - start;
}
tMap[s[start]]++;
console.log(tMap);
if (tMap[s[start]] > 0) {
counter++;
}
start++;
}
}
console.log(tMap);
if (minLen !== Infinity) {
shortestSubstring = s.substring(minStart, minStart + minLen);
}
return shortestSubstring;
};