@waigx Very smart solution using pos/neg counts and Counter.update(). I modified your code a little to make it more straightforward.

class Solution2(object): def findAnagrams(self, s, p): """ :type s: str :type p: str :rtype: List[int] """ ns, np, res = len(s), len(p), [] cnt = Counter(p) # positive count for p cnt.subtract(s[:np-1]) # negative count for s for i in range(np-1, ns): cnt.update({s[i]: -1}) # decrease window right i < np or cnt.update({s[i-np]: 1}) # increase window left if not any(cnt.values()): # check all letters count 0 res.append(i-(np-1)) return resSubcategories

Smarking Algorithm Contest