Valid Anagram
published on 4/28/2022Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"Output: trueExample 2:
Example 2:
Input: s = "rat", t = "car"Output: false
Approch 1
Sort the strings and compare if equal return True else return False
class Solution:def isAnagram(self, s: str, t: str) -> bool:return sorted(s) == sorted(t)
Approch 2
Use a hashmap to store the count of each character in the string.
After that use another for loop on the string 't' subtract frequency and check if frequency >0 return True else return False
class Solution:def isAnagram(self, s: str, t: str) -> bool:def isAnagram(self, s: str, t: str) -> bool:if len(s) != len(t):return Falsehashi = {}for char in s:if char not in hashi:hashi[char] = 1else:hashi[char] += 1for char in t:if char in hashi:hashi[char] -= 1else:return Falsefor val in hashi.values():if val != 0:return Falsereturn True
Approch 3
Use a 'dict' to store the count of each character in the given two string.
And the compare the two dict if they are equal return True else False.
class Solution:def isAnagram(self, s: str, t: str) -> bool:dic1, dic2 = {}, {}for item in s:dic1[item] = dic1.get(item, 0) + 1for item in t:dic2[item] = dic2.get(item, 0) + 1return dic1 == dic2