Valid Anagram

Problem

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

For example:

Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false

Solution

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        dic1, dic2 = {}, {}
        
        for letter in s:
            if letter not in dic1:
                dic1[letter] = 0
            dic1[letter]+=1
            
        for letter in t:
            if letter not in dic2:
                dic2[letter] = 0
            dic2[letter]+=1
        
        return dic1 == dic2

Last updated