Short Python Sliding Window


  • 0

    Here need is what we need to form a permutation of s1, and have is the characters we have in the current window.

    class Solution(object):
        def checkInclusion(self, s1, s2):
            """
            :type s1: str
            :type s2: str
            :rtype: bool
            """
            n1, n2 = len(s1), len(s2)
            need = collections.Counter(s1)
            have = collections.Counter(s2[:n1])
            for i in range(n2-n1):
                if need == have: return True
                have[s2[i]] -= 1
                have[s2[i+n1]] += 1
                if not have[s2[i]]: del have[s2[i]]
            return need == have
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.