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

来源:互联网 发布:哪里可以买到淘宝小号 编辑:程序博客网 时间:2024/09/21 08:15
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
原创粉丝点击