438. Find All Anagrams in a String

来源:互联网 发布:深入浅出mysql第三版 编辑:程序博客网 时间:2024/06/11 04:37
class Solution(object):
    def findAnagrams(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """
        res = []
        pCounter = collections.Counter(p)
        sCounter = collections.Counter(s[:len(p)-1])
        for i in range(len(p)-1,len(s)):
            sCounter[s[i]] += 1   
            if sCounter == pCounter:   
                res.append(i-len(p)+1)  
            sCounter[s[i-len(p)+1]] -= 1  
            if sCounter[s[i-len(p)+1]] == 0:
                del sCounter[s[i-len(p)+1]]

        return res


https://discuss.leetcode.com/topic/64412/python-sliding-window-solution-using-counter

原创粉丝点击