class Solution(object):
def isSame(self, a, b):
for i in range(0, len(a)):
if a[i] != b[i]: return False
return True
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
if not s or not p or len(s) < len(p): return []
a = ord('a')
# 计算字符串 p 中的字母数量
countP = [0 for _ in range(26)]
for c in p:
countP[ord(c) - a] += 1
# 在 s 中先滑出一个窗口,计算窗口中的字母数量
countS = [0 for _ in range(26)]
for k in range(0, len(p)):
countS[ord(s[k]) - a] += 1
ans = []
# 比较窗口中的字母数量和字符串 p 中的字母数量
if (self.isSame(countP, countS)):
ans.append(0)
# 继续向右滑动窗口,更新 -> 比较
i, j = 0, len(p) - 1
while j < len(s) - 1:
countS[ord(s[i]) - a] -= 1
i += 1
j += 1
countS[ord(s[j]) - a] += 1
if (self.isSame(countP, countS)):
ans.append(i)
return ans