class Solution {
public List<Integer> findAnagrams(String s, String p) {
int m = s.length();
int n = p.length();
List<Integer> res = new ArrayList<>();
if (n > m) {
return res;
}
int[] sCount = new int[26];
int[] pCount = new int[26];
// 統(tǒng)計(jì)p中出現(xiàn)字符個(gè)數(shù)
for (int i = 0; i < n; i++) {
pCount[p.charAt(i) - 'a']++;
}
// s字符雙指針滑動(dòng)
int left= 0;
for (int right = 0; right < m; right++) {
int rightIndex = s.charAt(right) - 'a';
sCount[rightIndex]++;
// s中的元素多于p時(shí)募谎,左邊彈出心墅,直到相等
while (sCount[rightIndex] > pCount[rightIndex]) {
int leftIndex = s.charAt(left) - 'a';
sCount[leftIndex]--;
left++;
}
// 每一個(gè)值都經(jīng)過比較,所以是相等的
if (right - left + 1 == n) {
res.add(left);
}
}
return res;
}
}