Use one dictionary in Python

  • 1
    class Solution(object):
        def isAnagram(self, s, t):
            :type s: str
            :type t: str
            :rtype: bool
            d1 = {} 
            for i in s:
                if i not in d1:
                    d1[i] = 1
                    d1[i] += 1
            for i in t:
                if i not in d1:
                    return False
                    d1[i] -= 1
            for key in d1:
                if d1[key] != 0:
                    return False
            return True

Log in to reply

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