https://leetcode.cn/problems/find-all-anagrams-in-a-string/description/?envType=study-plan-v2&envId=top-100-liked
双指针,每次都统计出来p长度的滑动窗口里的数字,拿Arrays.equals进行对比,然后滑动一小格,减1加1继续比对即可。
class Solution {public List<Integer> findAnagrams(String s, String p) {List<Integer> ans = new ArrayList<>();int sLen = s.length();int pLen = p.length();//小于直接返回空的if(sLen<pLen) return new ArrayList<>();int[] sWindows = new int[26];int[] pWindows = new int[26];//先把p里面的统计出来for(int i=0;i<pLen;i++){sWindows[s.charAt(i)-'a']++;pWindows[p.charAt(i)-'a']++;}if (Arrays.equals(sWindows, pWindows)) {ans.add(0);}for(int i=0;i<sLen-pLen;i++){sWindows[s.charAt(i)-'a']--;sWindows[s.charAt(i+pLen)-'a']++;if (Arrays.equals(sWindows, pWindows))ans.add(i + 1);}return ans;}
}