Python dictionary O(n) + O(n)


  • 0
    class Solution(object):
        def isAnagram(self, s, t):
            dic = {}
            for c in s:
                dic[c] = dic.get(c, 0) + 1
            for c in t:
                if c in dic:
                    dic[c] -= 1
                    if dic[c] == 0:
                        del dic[c]
                else:
                    return False
            return len(dic) == 0
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.