Skip to content

Commit 02e3222

Browse files
authored
Create 76-minimum-window-substring.js
1 parent ec920a0 commit 02e3222

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

76-minimum-window-substring.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
const minWindow = function(s, t) {
7+
const map = {}
8+
for (const c of t) {
9+
map[c] = (map[c] || 0) + 1
10+
}
11+
let counter = t.length
12+
let start = 0
13+
let end = 0
14+
let minLen = Infinity
15+
let minStart = 0
16+
while (end < s.length) {
17+
const eChar = s[end]
18+
if (map[eChar] > 0) {
19+
counter--
20+
}
21+
map[eChar] = (map[eChar] || 0) - 1
22+
end++
23+
while (counter === 0) {
24+
if (end - start < minLen) {
25+
minStart = start
26+
minLen = end - start
27+
}
28+
const sChar = s[start]
29+
map[sChar] = (map[sChar] || 0) + 1
30+
if (map[sChar] > 0) {
31+
counter++
32+
}
33+
start++
34+
}
35+
}
36+
if (minLen !== Infinity) {
37+
return s.substring(minStart, minStart + minLen)
38+
}
39+
return ''
40+
}

0 commit comments

Comments
 (0)