forked from jeantimex/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum-window-substring.js
56 lines (49 loc) · 1.32 KB
/
minimum-window-substring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Minimum Window Substring
*
* Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
*
* For example,
* S = "ADOBECODEBANC"
* T = "ABC"
* Minimum window is "BANC".
*
* Note:
* If there is no such window in S that covers all characters in T, return the empty string "".
*
* If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
*/
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
const minWindow = (s, t) => {
// Build a map and count characters in t
const map = {};
for (let c of t) {
map[c] = ~~map[c] + 1;
}
let start = 0;
let size = Infinity;
let counter = t.length;
// Try to find the window in s with two pointers i, j
for (let i = 0, j = 0; j < s.length; j++) {
// Step 1. count the character
if (map[s[j]]-- > 0) {
counter--; // Found a character in t
}
// Step 2. condition matched (the current window contains all the characters in t)
while (counter === 0) {
if (j - i + 1 < size) {
size = j - i + 1;
start = i;
}
if (map[s[i++]]++ === 0) {
counter++; // Make it invalid
}
}
}
return size === Infinity ? '' : s.substr(start, size);
};
export default minWindow;