-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path76_minimum-window-substring.cpp
More file actions
27 lines (26 loc) · 1.04 KB
/
Copy path76_minimum-window-substring.cpp
File metadata and controls
27 lines (26 loc) · 1.04 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
class Solution {
public:
string minWindow(string s, string t) {
int m=s.size();
map<char, int> ts;
for (auto &c : t) {
ts[c]++;
}
int needed = ts.size();
int first_take=0, next_take=0;
pair<int,int> sol({INT_MAX,0});
while (true) {
if (needed > 0) { // if we still need some chars
if (next_take == m) break;
if (--ts[s[next_take++]] == 0) needed--; // keep going right until we found all needed chars in right amount
} else {
if (ts[s[first_take++]]++ == 0) needed++; // if it was 0 before adding the char, we now lost one needed requirement
}
if (needed == 0 && sol.first>next_take-first_take) { // if we have all needed reqs and its smaller
sol={next_take-first_take,first_take}; // we assign it as new solution
}
}
if (sol.first==INT_MAX) return ""; // if we never found any solution
return s.substr(sol.second,sol.first);
}
};