76. Minimum Window Substring
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).
For example,
S = "ADOBECODEBANC"
T = "ABC"
Minimum window is "BANC".
class Solution {
public:
string minWindow(string s, string t) {
vector<int> map(128, 0);
for(auto c:t) map[c]++;
int b = 0, e = 0, dis = INT_MAX, head = 0;
int counter = t.size();
while(e < s.size()){
if(map[ s[e++] ]-- > 0) counter--;
while(counter == 0){
if(e - b < dis) {
dis = e-b;
head = b;
}
if(map[ s[b++] ]++ == 0 ) counter++;
}
}
return dis==INT_MAX? "":s.substr(head, dis);
}
};