Find All Anagrams in a String
from collections import Counter
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
pdict = dict(Counter(p))
S, P = len(s), len(p)
temp = {}
results = []
for ix in range(S):
if ix >= P:
if temp[s[ix - P]] == 1:
temp.pop(s[ix - P])
else:
temp[s[ix - P]] = temp[s[ix - P]] - 1
c = s[ix]
temp[c] = temp.get(c, 0) + 1
if pdict == temp:
results.append(ix - P + 1)
return results