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
                dic[x] = 1
        for x in t:
            if x not in dic or dic[x] ==0:
                return False
                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.