class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ans = left = 0
window = set() # 维护从下标 left 到下标 right 的字符
for right, c in enumerate(s):
while c in window: # 加入 c 后,窗口内会有重复元素
window.remove(s[left])
left += 1 # 缩小窗口
window.add(c)
ans = max(ans, right - left + 1) # 更新窗口长度最大值
return ans
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
s_len, p_len, res = len(s), len(p), []
counts = [0] * 26 # 长度26值为0的数组
# 存储p串的字符数量
for c_p in p:
counts[ord(c_p) - ord("a")] += 1
# 滑动窗口
l = 0
for r in range(s_len):
counts[ord(s[r]) - ord('a')] -= 1 # 记录
# 当右侧用完了,收缩左侧
while counts[ord(s[r]) - ord('a')] < 0:
counts[ord(s[l]) - ord('a')] += 1
l += 1
# [l, r]长度相同记录结果
if r - l + 1 == p_len:
res.append(l)
return res