【leetcode❤python】 438. Find All Anagrams in a String

class Solution(object):
    def findAnagrams(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """
        reslist=[];sdic={};pdic={}
        ls=len(s)
        lp=len(p)
        
        for i in s[:lp]:
            sdic[i]=sdic.get(i,0)+1
        for i in p[:lp]:
            pdic[i]=pdic.get(i,0)+1
        i=0
        while i<ls-lp:
            if sdic==pdic:reslist.append(i)
            sdic[s[i]]-=1
            if sdic[s[i]]==0:
                del sdic[s[i]]
            sdic[s[i+lp]]=sdic.get(s[i+lp],0)+1
            i+=1
        if sdic==pdic:
            reslist.append(i)
        
        return reslist

原文地址:https://www.cnblogs.com/kwangeline/p/6016928.html