Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

&nbsp;
Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".


Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".


&nbsp;
Constraints:


	1 &lt;= s.length, p.length &lt;= 3 * 104
	s and p consist of lowercase English letters.


from collections import Counter
class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        l='abcdefghijklmnopqrstuvwxyz'
        if len(p)>len(s):
            return []
        d={}
        for x in l:
            d[x]=0
        d1=dict(d)
        d2=dict(d)
        for x in range(len(p)):
            d1[s[x]]+=1
            d2[p[x]]+=1
        l1=[]
        if d1==d2:
            l1=[0]
        #print(d1)
        for x in range(len(p),len(s)):
            d1[s[x]]+=1
            d1[s[x-len(p)]]-=1
            if d1==d2:
                l1.append(x-len(p)+1)
        return l1
class Solution { 
    
    public List<Integer> findAnagrams(String s, String p) {
        int fullMatchCount = p.length();
        Map<Character, Integer> anagramMap = new HashMap<>();
        
        for (Character c : p.toCharArray())
            anagramMap.put(c, anagramMap.getOrDefault(c, 0) + 1);
        
        List<Integer> result = new ArrayList<>();
        int left = 0, right = 0, currentMatchCount = 0;
        Map<Character, Integer> currentAnagramMap = new HashMap<>();
        while (right < s.length()) {
            char c = s.charAt(right);
            if (anagramMap.get(c) == null) {
                currentAnagramMap = new HashMap<>();
                right++;
                left = right;
                currentMatchCount = 0;
                continue;
            }
            currentAnagramMap.put(c, currentAnagramMap.getOrDefault(c, 0) + 1);
            currentMatchCount++;
            
            if (currentAnagramMap.get(c) > anagramMap.get(c)) {
                char leftC = s.charAt(left);
                while (leftC != c) {
                    currentAnagramMap.put(leftC, currentAnagramMap.get(leftC) - 1);
                    left++;
                    leftC = s.charAt(left);
                    currentMatchCount--;
                }
                left++;
                currentAnagramMap.put(c, currentAnagramMap.get(c) - 1);
                currentMatchCount--;
            }
            
            if (currentMatchCount == fullMatchCount)
                result.add(left);
            
            right++;
        }
        return result;
    }
}
//easy to understand
class Solution {
public:
    bool allZeros(vector<int> &count) {
        for (int i = 0; i < 26; i++) {
            if (count[i] != 0) 
                return false;
        }
        return true;
    }
    vector<int> findAnagrams(string s, string p) {
        string s1 = p, s2 = s;
        int n = s1.length(); 
        int m = s2.length();
        if (n > m) 
            return {};

        vector<int> ans;
        vector<int> count(26, 0);

        for (int i = 0; i < n; i++) {
            count[s1[i] - 'a']++;
            count[s2[i] - 'a']--;
        }
        //it will check for s1 = abcd, s2 = cdab
        if (allZeros(count)) {
            ans.push_back(0);
        }

        for (int i = n; i < m; i++) {
            count[s2[i] - 'a']--;
            count[s2[i - n] - 'a']++;
            if (allZeros(count)) 
                ans.push_back(i-n+1);
        }

        return ans;
    }
};
var findAnagrams = function(s, p) {

    function compareMaps(map1, map2) {
    var testVal;
    if (map1.size !== map2.size) {
        return false;
    }
    for (var [key, val] of map1) {
        testVal = map2.get(key);
        // in cases of an undefined value, make sure the key
        // actually exists on the object so there are no false positives
        if (testVal !== val || (testVal === undefined && !map2.has(key))) {
            return false;
        }
    }
    return true;
    }

    let p_map=new Map()
    let s_map=new Map()
    let res=[]
    let pn=p.length
    let sn=s.length
    for(let i in p){
        p_map.set(p[i],p_map.get(p[i])?p_map.get(p[i])+1:1)
        s_map.set(s[i],s_map.get(s[i])?s_map.get(s[i])+1:1)
    }
    let l=0
    if(compareMaps(s_map,p_map)) res.push(l)
    for(let r=pn;r<sn;r++){
        s_map.set(s[r],s_map.get(s[r])?s_map.get(s[r])+1:1)
        s_map.set(s[l],s_map.get(s[l])-1)
        if(s_map.get(s[l])===0){
            s_map.delete(s[l])
        }
        if(compareMaps(s_map,p_map)) res.push(l+1)
        l++
    }
    return(res)

};
Find All Anagrams in a String