【python刷题】滑动窗口法

模板

left,right = 0,0
while right < len(s):
    windows.append(s[right])
    right += 1
    while (windows needs shrink):
        window.pop(0)
        left += 1

leetcode 76 最小覆盖子串

class Solution:
    def minWindow(self, s: str, t: str) -> str:
        from collections import Counter
        needs = Counter(t)
        windows = {i:0 for i in t}
        left, right = 0, 0
        valid = 0
        # 记录最小覆盖子串的起始位置以及长度
        start = 0
        leng = float("inf")
        while right < len(s):
            c = s[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while valid == len(needs):
                if right - left < leng:
                    start = left
                    leng = right - left
                d = s[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return "" if leng == float("inf") else s[start:start+leng]

leetcode 567 字符串排列

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        from collections import Counter
        needs = Counter(s1)
        windows = {i:0 for i in s1}
        left, right = 0, 0
        valid = 0
        while right < len(s2):
            c = s2[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while right - left > len(s1) - 1: # 窗口中只能有len(s1)个元素,
                if valid == len(needs):
                    print(s2[left:right])
                    return True
                d = s2[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return False

leetcode 438 找所有字母异位词

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        from collections import Counter
        needs = Counter(p)
        windows = {i:0 for i in p}
        left, right = 0, 0
        valid = 0
        res = []
        while right < len(s):
            c = s[right]
            right += 1
            if c in needs:
                windows[c] += 1
                if needs[c] == windows[c]:
                    valid += 1
            while right - left > len(p) - 1: # 窗口中只能有两个元素,
                if valid == len(needs):
                    print(s[left:right])
                    res.append(left)
                d = s[left]
                left += 1
                if d in needs:
                    if windows[d] == needs[d]:
                        valid -= 1
                    windows[d] -= 1
        return res

leetcode 3 最长无重复子串

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if len(s) == 0:
            return 0
        if len(s) == 1:
            return 1
        from collections import defaultdict
        windows = defaultdict(int)
        left, right = 0,0
        res = float("-inf")
        while right < len(s):
            c = s[right]
            right += 1
            windows[c] += 1
            while windows[c] > 1:
                d = s[left]
                left += 1
                windows[d] -= 1
            res = max(res, right - left)
        return res
原文地址:https://www.cnblogs.com/xiximayou/p/14368112.html