-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path76. Minimum Window Substring.ts
53 lines (43 loc) · 1.13 KB
/
76. Minimum Window Substring.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
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
/**
* Runtime 318 ms Beats 19.23%
* Memory 48.9 MB Beats 41.83%
*/
const isSameCount = (
sMap: Record<string, number>,
tMap: Record<string, number>,
tKeys: string[]
): boolean => {
for (const key of tKeys) {
if (!sMap[key] || tMap[key] > sMap[key]) {
return false;
}
}
return true;
};
function minWindow(s: string, t: string): string {
const tMap = t.split('').reduce<Record<string, number>>((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
}, {});
const tKeys = Object.keys(tMap);
const sMap: Record<string, number> = {};
let minimumSubstring = s + t;
let i = 0;
for (let j = 0; j < s.length; j++) {
sMap[s[j]] = (sMap[s[j]] || 0) + 1;
while (isSameCount(sMap, tMap, tKeys)) {
const subString = s.slice(i, j + 1);
minimumSubstring =
minimumSubstring.length > subString.length
? subString
: minimumSubstring;
sMap[s[i]] -= 1;
if (sMap[s[i]] === 0) {
delete sMap[s[i]];
}
i++;
}
}
return minimumSubstring === s + t ? '' : minimumSubstring;
}
console.log(minWindow('ADOBECODEBANC', 'ABC')); // BANC