Python solution using dictionary


  • 0

    class Solution(object):
    def isAnagram(self, s, t):
    """
    :type s: str
    :type t: str
    :rtype: bool
    """
    if not s:
    return not t
    if len(s) != len(t):
    return False

        dic = {}
        for x in s:
            if x in dic:
                dic[x] = dic[x]+ 1
            else:
                dic[x] = 1
                
        for x in t:
            if x not in dic or dic[x] ==0:
                return False
            else:
                dic[x] = dic[x] -1
        return True

Log in to reply
 

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