-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
92 lines (90 loc) · 2.41 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode id=76 lang=javascript
*
* [76] Minimum Window Substring
*
* https://leetcode.com/problems/minimum-window-substring/description/
*
* algorithms
* Hard (29.62%)
* Total Accepted: 216.3K
* Total Submissions: 720.7K
* Testcase Example: '"ADOBECODEBANC"\n"ABC"'
*
* Given a string S and a string T, find the minimum window in S which will
* contain all the characters in T in complexity O(n).
*
* Example:
*
*
* Input: S = "ADOBECODEBANC", T = "ABC"
* Output: "BANC"
*
*
* Note:
*
*
* If there is no such window in S that covers all characters in T, return the
* empty string "".
* If there is such window, you are guaranteed that there will always be only
* one unique minimum window in S.
*
*/
/**
* 思路:
*
* 1、用两个指针记录子串的起始位置和结束位置
* 2、再用一个hash表记录这中间字符出现的次数
* 3、如果出现的次数能够构成t,记录此时的结果,并保存每次最小的结果
* 4、则将left向右缩小到不能再缩为止
* 5、返回上面遍历的最小结果
*
* @param {string} s
* @param {string} t
* @return {string}
*/
var minWindow = function(s, t) {
const tMap = t.split('').reduce((res, char) => {
if (res[char]) {
res[char] ++;
} else {
res[char] = 1;
}
return res;
}, {});
const sMap = {};
let curResCount = 0;
let result = '';
let left = 0;
for (let i = 0; i < s.length; i++) {
const curChar = s[i];
sMap[curChar] = sMap[curChar] === undefined ? 1 : sMap[curChar] + 1;
if (
tMap[curChar] &&
sMap[curChar] <= tMap[curChar]
) {
curResCount++;
}
while (curResCount === t.length) {
if (result === '' || result.length > i - left + 1) {
result = s.slice(left, i + 1);
}
const leftChar = s[left];
sMap[leftChar]--;
if (tMap[leftChar] && sMap[leftChar] < tMap[leftChar]) {
curResCount--;
}
left++;
}
}
return result
};
console.log(minWindow('ADOBECODEBANC', 'ABC'));
console.log(minWindow('bba', 'ab'));
console.log(minWindow('abba', 'aba'));
module.exports = {
id:'76',
title:'Minimum Window Substring',
url:'https://leetcode.com/problems/minimum-window-substring/description/',
difficulty:'Hard',
}