forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
28 lines (28 loc) · 804 Bytes
/
Solution.ts
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
function minWindow(s: string, t: string): string {
const need: number[] = new Array(128).fill(0);
const window: number[] = new Array(128).fill(0);
for (const c of t) {
++need[c.charCodeAt(0)];
}
let cnt = 0;
let j = 0;
let k = -1;
let mi = 1 << 30;
for (let i = 0; i < s.length; ++i) {
++window[s.charCodeAt(i)];
if (need[s.charCodeAt(i)] >= window[s.charCodeAt(i)]) {
++cnt;
}
while (cnt === t.length) {
if (i - j + 1 < mi) {
mi = i - j + 1;
k = j;
}
if (need[s.charCodeAt(j)] >= window[s.charCodeAt(j)]) {
--cnt;
}
--window[s.charCodeAt(j++)];
}
}
return k < 0 ? '' : s.slice(k, k + mi);
}