class Solution {
public:
vector<int> findAnagrams(string s, string p) {
#define nums(i) (i - 'a')
vector<int> ans;
int n = s.size(), m = p.size();
if (n < m) {
return ans;
}
int cnt[26]{};
for (char c: p) {
cnt[nums(c)]++;
}
int l = 0, r;
for (r = 0; r < n; r++) {
int c = s[r];
cnt[nums(c)]--;
// the cnt of c is too big
while (cnt[nums(c)] < 0) {
// move l ->
cnt[nums(s[l])]++;
l++;
}
// if size of window == m
// then it is an anagram of p
if (r - l + 1 == m) {
ans.push_back(l);
}
}
return ans;
#undef nums(i)
}
};