forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0076.cpp
More file actions
31 lines (31 loc) · 740 Bytes
/
0076.cpp
File metadata and controls
31 lines (31 loc) · 740 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
class Solution {
public:
string minWindow(string &s, string &t) {
std::unordered_map<char, int> target, f;
for (auto &c : t)
target[c]++;
auto valid{[&]() {
for (const auto & [ key, val ] : target) {
if (val > f[key])
return false;
}
return true;
}};
int n = s.length();
int minStart{0}, minLength{0}, start{0}, end{0};
while (end < n) {
char c{s[end]};
f[c]++;
while (valid() and start <= end) {
if (minLength == 0 or minLength > end - start + 1) {
minStart = start;
minLength = end - start + 1;
}
f[s[start]]--;
start++;
}
end++;
}
return s.substr(minStart, minLength);
}
};