class Solution {
public List<Integer> findAnagrams(String s, String p) {
int len1 = s.length();
int len2 = p.length();
List<Integer> list = new ArrayList<>();
int[] sign = new int[26];
if(len1 < len2){
return list;
}
for(int i = 0; i < len2; i++){
sign[p.charAt(i) - 'a']++;
}
int left = 0;
int right = 0;
for(right = left; right < len1; right++){
int index = s.charAt(right) - 'a';
sign[index]--;
while(sign[index] < 0){
sign[s.charAt(left) - 'a']++;
left++;
}
if(right - left + 1 == len2){
list.add(left);
}
}
return list;
}
}
class Solution {
public List<Integer> findAnagrams(String s, String p) {
int len1 = s.length();
int len2 = p.length();
List<Integer> list = new ArrayList<>();
if(len1 < len2){
return list;
}
int[] ssign = new int[26];
int[] psign = new int[26];
for(int i = 0; i < len2; i++){
psign[p.charAt(i) - 'a']++;
ssign[s.charAt(i) - 'a']++;
}
if (Arrays.equals(ssign, psign)) {
list.add(0);
}
for(int i = 0; i < len1 - len2; i++){
ssign[s.charAt(i) - 'a']--;
ssign[s.charAt(i + len2) - 'a']++;
if (Arrays.equals(ssign, psign)) {
list.add(i + 1);
}
}
return list;
}
}