forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
28 lines (23 loc) · 661 Bytes
/
index.ts
File metadata and controls
28 lines (23 loc) · 661 Bytes
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
// HELP:
export function minWindow(S: string, T: string) {
const HASH = Array(128).fill(0)
let [left, right, head] = [0, 0, 0]
let minLength = Infinity
let counter = T.length
for (let c of T) HASH[c.charCodeAt(0)]++
while (right < S.length) {
if (HASH[S.charCodeAt(right)] > 0) counter--
HASH[S.charCodeAt(right)]--
right++
while (counter === 0) {
if (right - left < minLength) {
head = left
minLength = right - left
}
if (HASH[S.charCodeAt(left)] === 0) counter++
HASH[S.charCodeAt(left)]++
left++
}
}
return minLength === Infinity ? '' : S.slice(head, head + minLength)
}