-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY 14
More file actions
35 lines (30 loc) · 888 Bytes
/
DAY 14
File metadata and controls
35 lines (30 loc) · 888 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
29
30
31
32
33
34
35
class Solution {
public:
std::string minWindow(std::string s, std::string t) {
if (s.empty() || t.empty() || s.length() < t.length()) {
return "";
}
std::vector<int> map(128, 0);
int count = t.length();
int start = 0, end = 0, minLen = INT_MAX, startIndex = 0;
/// UPVOTE !
for (char c : t) {
map[c]++;
}
while (end < s.length()) {
if (map[s[end++]]-- > 0) {
count--;
}
while (count == 0) {
if (end - start < minLen) {
startIndex = start;
minLen = end - start;
}
if (map[s[start++]]++ == 0) {
count++;
}
}
}
return minLen == INT_MAX ? "" : s.substr(startIndex, minLen);
}
};