您的位置:首页 > 其它

438. Find All Anagrams in a String

2017-06-09 14:50 483 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: