My idea to solve the problem is that make 2 vector for length of string add the inital freq
vector<int> findAnagrams(string s, string p) {
vector<int> ans;
vector<int> counts(26,0), countp(26,0);
if(s.size() < p.size())return ans;
for(int i = 0 ; i < p.size() ; i++){
counts[s[i]-'a']+=1;
countp[p[i]-'a']+=1;
}
int m = p.size() , n = s.size();
for(int i = p.size() ; i < s.size() ; i++){
if(counts == countp){
ans.push_back(i-m);
}
counts[s[i]-'a']+=1;
counts[s[i-m]-'a']--;
}
if(counts == countp)
ans.push_back(n-m);
return ans;
}
now do it like sliding window approach
start from size of pattern string
compare the comp_s with comp_p
if equal add pos i-size of ans vector.
then increment freq of the right pointer character simultaneously decrementing freq of the leftmost
character i-p.size()
you`ll make out a lot from this code.
in case of further doubt ping me