class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
Map<Character,Integer> need = new HashMap<>();
Map<Character,Integer> window = new HashMap<>();
for(char c : p.toCharArray()){
need.put(c,need.getOrDefault(c,0)+1);
}
int left = 0;
int right = 0;
int vaild = 0;
while(right < s.length()){
char c = s.charAt(right);
right++;
if(need.containsKey(c)){
window.put(c,window.getOrDefault(c,0)+1);
if(window.get(c).equals(need.get(c))){
vaild++;
}
}
while(right-left>=p.length()){
if(vaild==need.size()){
res.add(left);
}
char d = s.charAt(left);
left++;
if(need.containsKey(d)){
if(window.get(d).equals(need.get(d))){
vaild--;
}
window.put(d,window.getOrDefault(d,0)-1);
}
}
}
return res;
}
}