567. Permutation in String (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def checkInclusion(self, s1: str, s2: str) -> bool:
        n1, n2 = len(s1), len(s2)
        if n1 > n2:
            return False
        h1 = Counter(s1)
        h2 = defaultdict(int)
        i = j = 0
        while j < n1:
            h2[s2[j]] += 1
            j += 1
        while j < n2:
            if h1 == h2:
                return True
            h2[s2[j]] += 1
            h2[s2[i]] -= 1
            if not h2[s2[i]]:
                del h2[s2[i]]
            i += 1
            j += 1
        return h1 == h2