Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example, s= "anagram",t= "nagaram", return true. s= "rat",t= "car", return false.

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        """
        return sorted(s) == sorted(t)
        """
        dic1, dic2 = {}, {}
        for item in s:
            dic1[item] = dic1.get(item, 0) + 1
        for item in t:
            dic2[item] = dic2.get(item, 0) + 1
        return dic1 == dic2

Last updated

Was this helpful?