讨论/《零起步学算法》 - 练习:字符串的排列/
《零起步学算法》 - 练习:字符串的排列

python解法:与最小覆盖子串思路相同

class Solution(object):
    def findAnagrams(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """
        need, window = {}, {}
        left, right = 0, 0
        start, end = 0 ,0
        vaild = 0
        ans = []
        for i in p:
            need[i] = need.setdefault(i, 0) + 1
        while right < len(s):
            c = s[right]
            right += 1
            if c in need:
                window[c] = window.setdefault(c, 0) + 1
                if window[c] == need[c]:
                    vaild += 1
            while right - left >= len(p):
                if vaild == len(need):
                    ans.append(left)
                d = s[left]
                left += 1
                if d in need:
                    if window[d] == need[d]:
                        vaild -= 1
                    window[d] -= 1
        return ans