-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0076-minimum-window-substring.js
44 lines (40 loc) · 1.11 KB
/
0076-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
/**
* 76. Minimum Window Substring
* https://leetcode.com/problems/minimum-window-substring/
* Difficulty: 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.
*/
/**
* @param {string} s
* @param {string} t
* @return {string}
*/
var minWindow = function(s, t) {
const values = new Array(128).fill(0);
let [start, end] = [-Infinity, Infinity];
for (let i = 0; i < t.length; i++) {
values[t.charCodeAt(i)]++;
}
for (let i = 0, j = 0, total = t.length; i < s.length; i++) {
if (values[s.charCodeAt(i)] > 0) {
total--;
}
values[s.charCodeAt(i)]--;
while (!total) {
if (end - start > i - j) {
[start, end] = [j, i];
}
values[s.charCodeAt(j)]++;
if (values[s.charCodeAt(j)] > 0) {
total++;
}
j++;
}
}
return end !== Infinity ? s.slice(start, end + 1) : '';
};