算法分析与设计第十九周: 567. Permutation in String

class Solution(object):
    def checkInclusion(self, s1, s2):
        l1 = [0 for _ in range(26)]
        l2 = [0 for _ in range(26)]
        size1 = len(s1)
        size2 = len(s2)
        if size2 < size1:
            return False
        if size1 == 0:
            return True

        charSet = set()
        for c in s1:
            l1[ord(c) - ord('a')] += 1
            charSet.add(ord(c) - ord('a'))

        for i in range(size1):
            l2[ord(s2[i]) - ord('a')] += 1

        hasFound = True
        for c in charSet:
            if l1[c] != l2[c]:
                hasFound = False
        if hasFound:
            return True

        i = size1
        while i < size2:
            l2[ord(s2[i - size1]) - ord('a')] -= 1
            l2[ord(s2[i]) - ord('a')] += 1
            hasFound = True
            for c in charSet:
                if l1[c] != l2[c]:
                    hasFound = False
            if hasFound:
                return True

            i += 1
        return False

你可能感兴趣的:(算法,算法)