思路,滑动窗口,注意比较的是每一个窗口之中每个字母的出现次数和a一样就行了,不用管顺序
class Solution {
public:
int timesA[26];
int timesB[26];
int l1, l2;
bool checkInclusion(string s1, string s2) {
memset(timesA, 0, sizeof(timesA));
memset(timesB, 0, sizeof(timesB));
l1 = s1.size();
l2 = s2.size();
if(l1 > l2){
return false;
}
for(int i = 0; i < l1; i++){
timesA[s1[i] - 'a']++;
}
for(int i = 0; i < l1 - 1; i++){
timesB[s2[i] - 'a']++;
}
int s = 0;
int e = l1 - 1;
while(e < l2){
timesB[s2[e] - 'a']++;
if(checkNowState()){
return true;
}
e++;
timesB[s2[s] - 'a']--;
s++;
}
return false;
}
bool checkNowState(){
for(int i = 0; i < 26; i++){
if(timesA[i] != timesB[i]){
return false;
}
}
return true;
}
};