这种思路就是固定窗口大小,然后不断向后滑动,直到找到一个排列相等的字符串
class Solution {
public:
bool checkInclusion(string s1, string s2) {
if(s1.size()>s2.size())
return false;
vector<int>need(26,0);
vector<int>have(26,0);
int len=s1.size();
for(int i=0;i<len;i++){
need[s1[i]-'a']++;
have[s2[i]-'a']++;
}
if(need==have)
return true;
int n=len;
while(n<s2.size()){
have[s2[n]-'a']++;
have[s2[n-len]-'a']--;
if(need==have)
return true;
n++;
}
return false;
}
};
这种思路就是双指针滑动窗口,快指针先走,达到一定条件就缩短窗口,反复执行
class Solution {
public:
bool checkInclusion(string s1, string s2) {
vector<int>need(26,0);
for(auto ch:s1)
need[ch-'a']++;
int fast=0,slow=0;
while(fast<s2.size()){
need[s2[fast]-'a']--;
while(need[s2[fast]-'a']<0){
need[s2[slow]-'a']++;
slow++;
}
if(fast-slow+1==s1.size()){
return true;
}
fast++;
}
return false;
}
};