# 判断左侧窗口是否要收缩 while left < right and"window needs shrink": # d 是将移出窗口的字符 d = s[left] window[d] -= 1 if window[d] == 0: del window[d] # 缩小窗口 left += 1 # 进行窗口内数据的一系列更新 #...
classSolution: defminWindow(self, s: str, t: str) -> str: window = defaultdict(int) need = defaultdict(int) for ch in t: need[ch] += 1
left, right = 0, 0 start, length = 0, float("inf") valid = 0 while right < len(s): c = s[right] right += 1 # window[c] += 1 # print(window) if c in need: window[c] += 1 if window[c] == need[c]: valid += 1 while valid == len(need): # 只有字符串字符串长度更短才会进行更新 if right - left < length: start, length = left, right - left d = s[left] left += 1 if d in need: if window[d] == need[d]: valid -= 1 window[d] -= 1 # print(window) return""if length == float("inf") else s[start:start + length]
if __name__ == '__main__': s = "ADOBECODEBANC" t = "ABC" print(Solution().minWindow(s, t))
3、无重复字符的最长子串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: deflengthOfLongestSubstring(self, s: str) -> int: length = 0 left, right = 0, 0 window = defaultdict(int) while right < len(s): c = s[right] right += 1 window[c] += 1
while window[c] > 1: d = s[left] left += 1 window[d] -= 1 length = max(length, right - left) return length
classSolution: defcheckInclusion(self, s1: str, s2: str) -> bool: window, need = defaultdict(int), defaultdict(int) for ch in s1: need[ch] += 1 left, right = 0, 0 valid = 0 while right < len(s2): c = s2[right] right += 1 if c in need: window[c] += 1 if window[c] == need[c]: valid += 1 while right - left >= len(s1): if valid == len(need): returnTrue d = s2[left] left += 1 if d in need: if window[d] == need[d]: valid -= 1 window[d] -= 1 returnFalse
if __name__ == '__main__': s = Solution() print(s.checkInclusion(s1="ab", s2="eidbcaooo"))
from collections import defaultdict from typing import List
classSolution: deffindAnagrams(self, s: str, p: str) -> List[int]: window, need = defaultdict(int), defaultdict(int) left, right = 0, 0 valid = 0 res = [] for ch in p: need[ch] += 1 while right < len(s): c = s[right] right += 1 if c in need: window[c] += 1 if window[c] == need[c]: valid += 1 while right - left >= len(p): if valid == len(need): res.append(left) d = s[left] window[d] -= 1 left += 1 if d in need: if window[d] == need[d]: valid -= 1 window[d] -= 1 return res
if __name__ == '__main__': s = "cbaebabacd" p = "abc" print(Solution().findAnagrams(s, p))
classSolution: defmaxSatisfied( self, customers: List[int], grumpy: List[int], minutes: int ) -> int: """ :param customers:顾客数量 :param grumpy: 生气 :param minutes: 老板可以保持这个分钟数不生气 :return: """ res = 0 for i inrange(len(customers)): if grumpy[i] == 0: res += customers[i] customers[i] = 0 # print(customers) # print(res) max_res = float("-inf") cur = 0 left, right = 0, 0 while right < len(customers): # if right - left < minutes: cur += customers[right] # print(queue) # queue.append(customers[right]) right += 1 if right - left > minutes: cur -= customers[left] left += 1 # queue.popleft() max_res = max(max_res, cur) # print(max_res) return res + max_res