-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_0076.cpp
More file actions
45 lines (41 loc) · 1.21 KB
/
lc_0076.cpp
File metadata and controls
45 lines (41 loc) · 1.21 KB
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
/**
* @file lc_0076.cpp
* @brief https://leetcode-cn.com/problems/minimum-window-substring/
* @author YongDu
* @date 2021-09-07
*/
class Solution {
using charMap = std::unordered_map<char, int>;
public:
string minWindow(string s, string t) {
charMap dstMap;
charMap curMap;
for (auto& ch : t) {
dstMap[ch]++;
}
int minLen = INT_MAX; // 最小长度
int leftBound = -1; // 最小长度左边界,最后截取子串
int left = 0;
for (int right = 0; right < s.size(); ++right) {
curMap[s[right]]++;
while (isValidSubStr(curMap, dstMap) && left <= right) {
if (right - left + 1 < minLen) {
minLen = right - left + 1;
leftBound = left;
}
--curMap[s[left]];
left++;
}
}
return minLen == INT_MAX ? string{} : s.substr(leftBound, minLen);
}
private:
bool isValidSubStr(charMap& curMap, charMap& dstMap) {
for (auto& [ch, num] : dstMap) {
if (curMap[ch] < num) {
return false;
}
}
return true;
}
};