438. Find All Anagrams in a String
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
target = Counter(list(p))
visited = {}
left = 0
right = 0
valid = 0
ans = []
while right < len(s):
c = s[right]
right += 1
if c in target:
visited[c] = visited.get(c, 0) + 1
if visited[c] == target[c]:
valid += 1
while right - left >= len(p):
if valid == len(target):
ans.append(left)
c = s[left]
left += 1
if c in target:
if visited[c] == target[c]:
valid -= 1
visited[c] -= 1
return ansLast updated