/*
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引。
字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100。*/
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<>();
if (s == null || p == null || s.length() < p.length()){
return res;
}
int sLen = s.length(), pLen = p.length();
int[] letters = new int[256];
for (char c : p.toCharArray()) {
letters[c - 'a']++;
}
int start = 0, end = 0, count = 0;
while (end < sLen) {
if (end - start == pLen && letters[s.charAt(start++) - 'a']++ >= 0) {
count--;
}
if (--letters[s.charAt(end++) - 'a'] >= 0) {
count++;
}
if (count == pLen) res.add(start);
}
return res;
}
}