【438】找到字符串中所有字母異位詞

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        p_d, s_d, leng, res,l={}, {}, len(p), [], 0
        for i in p: p_d[i]=p_d.get(i,0)+1
        for r, c in enumerate(s):
            s_d[c] = s_d.get(c,0)+1
            if c not in p_d:
                l = r+1
                s_d.clear()
            else:
                if r-l+1==leng:
                    if s_d==p_d:
                        res.append(l)
                    s_d[s[l]]-=1
                    l+=1
        return res

本題和567題類似,代碼改一下即可:
【leetcode 567】字符串的排列

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章