leetcode-242-有效的字母异位词

题目描述:

 

 方法一:排序 O(nlogn)

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        return sorted(s)==sorted(t)

方法二:哈希表 O(N) O(1)

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if len(s)!=len(t):
            return False
        dicts = collections.defaultdict(int)
        for i in range(len(s)):
            dicts[s[i]] += 1
            dicts[t[i]] -= 1
        for i in dicts:
            if dicts[i]!=0:
                return False
        return True

 

posted @ 2019-10-08 10:25  oldby  阅读(94)  评论(0编辑  收藏  举报