题目链接:438. 找到字符串中所有字母异位词
滑动窗口。
from collections import Counter
from typing import List
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
pc, l, res, sc = Counter(p), len(p), list(), Counter(s[:len(p)])
if sc == pc:
res.append(0)
for i in range(l, len(s)):
sc[s[i - l]] = sc.get(s[i - l]) - 1
if sc[s[i - l]] == 0:
sc.pop(s[i - l])
sc[s[i]] = sc.get(s[i], 0) + 1
if sc == pc:
res.append(i - l + 1)
return res