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

时间:2022-02-23 18:41:06

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