有效的字母異位詞

collections中的Counter真是香。。。

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        
        from collections import Counter
        sCounter = Counter([i for i in s])
        tCounter = Counter([i for i in t])
        
        if len(sCounter) != len(tCounter):
            return False
        
        for key in sCounter.keys():
            try:
                if sCounter[key] != tCounter[key]:
                    return False
            except:
                return False
        
        return True

 

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