跟着别人的代码履了一遍,明天自己再重写遍。
class Solution {
public:
map<char, int> tstr, sstr;
bool isContained(){
for(auto tchar : tstr){
if(tchar.second > sstr[tchar.first]) return false;
}
return true;
}
string minWindow(string s, string t) {
int n1 = s.size(), n2 = t.size();
if(n1 < n2) return string("");
int minLength = INT_MAX;
int ansL = -1;
for(char tchar : t){
++tstr[tchar];
}
int left = 0, right = 0;
for(; right < n1; ++right){
++sstr[s[right]];
if(tstr.find(s[right]) != tstr.end()){
while(isContained() && left <= right){
if(minLength > right - left + 1){
ansL = left;
minLength = right - left + 1;
}
--sstr[s[left++]];
}
}
}
if(ansL == -1) return string("");
return s.substr(ansL, minLength);
}
};
标签:子串,right,return,string,最小,tchar,76,tstr,left
From: https://www.cnblogs.com/llllmz/p/18414812