c1:记录t中字符的频率
c2:记录当前滑动窗口内字符的频率
class Solution {
public:
string minWindow(string s, string t) {
int tot = 0;
vector<int> c1(60), c2(60);
for(char x : t){
c1[getIdx(x)]++;
if(c1[getIdx(x)] == 1) tot++;
}
string ans = "";
int i = 0;
for (int j = 0; j < s.size(); j++){
int right = getIdx(s[j]);
c2[right]++;
if(c2[right] == c1[right]) tot--;
while (i < j) {
int left = getIdx(s[i]);
if (c2[left] > c1[left]) {
c2[left]--;
i++;
}
else break;
}
if (tot == 0 && (ans.empty() || ans.size() > j - i + 1)) ans = s.substr(i, j - i + 1);
}
return ans;
}
int getIdx(char x) {
return x >= 'A' && x <= 'Z' ? x - 'A' + 26 : x - 'a';
}
};
class Solution {
public:
string minWindow(string s, string t) {
int tot = 0;
vector<int> c1(60), c2(60);
for(char x : t){
c1[getIdx(x)]++;
if(c1[getIdx(x)] == 1) tot++;
}
string ans = "";
int i = 0;
for (int j = 0; j < s.size(); j++){
int right = getIdx(s[j]);
c2[right]++;
if(c2[right] == c1[right]) tot--;
while (i < j && c2[getIdx(s[i])] > c1[getIdx(s[i])]){
c2[getIdx(s[i])]--;
i++;
}
// while (i < j) {
// int left = getIdx(s[i]);
// if (c2[left] > c1[left]) {
// c2[left]--;
// i++;
// }
// else break;
// }
if (tot == 0 && (ans.empty() || ans.size() > j - i + 1)) ans = s.substr(i, j - i + 1);
}
return ans;
}
int getIdx(char x) {
return x >= 'A' && x <= 'Z' ? x - 'A' + 26 : x - 'a';
}
};