Python AC slide window solution with collections.Counter

  • 0

    The idea is keep a slide window for the counter, return true if all the values are 0.

    from collections import Counter
    class Solution(object):
        def checkInclusion(self, s1, s2):
            :type s1: str
            :type s2: str
            :rtype: bool
            start, end, window_size = 0, 0, len(s1)
            count = Counter(s1)
            while end < len(s2):            
                if s2[end] in count:
                    count[s2[end]] -= 1
                if end - start + 1 > window_size:
                    if s2[start] in count:
                        count[s2[start]] += 1
                    start += 1
                if not list(filter(lambda x: x > 0, count.values())):
                    return True
                end += 1
            return False

Log in to reply

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