My python solution with defaultdict (88ms) -- easy to understand

  • 0
     from collections import defaultdict
     class Solution(object):
        def isAnagram(self, s, t):
            sdic = defaultdict(int)
            tdic = defaultdict(int)
            if not s and not t:
                return True
            elif (len(s) != len(t)):
                return False
                for i in range(0, len(s)):
                    sdic[(ord(s[i]))] += 1
                    tdic[(ord(t[i]))] += 1
                for i in s:
                    if sdic[ord(i)] != tdic[ord(i)]:
                        return False
                return True

Log in to reply

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