""" 0242.1 - Valid Anagram - Solution 1 - Hash Map (Counter) """
#####################################################################################
# Imports
#####################################################################################
from collections import Counter
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isAnagram(self, s: str, t: str) -> bool:
"""Valid Anagram Function"""
if len(s) != len(t):
return False
return Counter(s) == Counter(t)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isAnagram("anagram", "nagaram")) # True
print(Solution().isAnagram("rat", "car")) # False
print(Solution().isAnagram("a", "ab")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0242.2 - Valid Anagram - Solution 2 - Manual Frequency Count """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isAnagram(self, s: str, t: str) -> bool:
"""Valid Anagram Function"""
if len(s) != len(t):
return False
count_s = {}
count_t = {}
for i in range(len(s)):
count_s[s[i]] = count_s.get(s[i], 0) + 1
count_t[t[i]] = count_t.get(t[i], 0) + 1
return count_s == count_t
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isAnagram("anagram", "nagaram")) # True
print(Solution().isAnagram("rat", "car")) # False
print(Solution().isAnagram("a", "ab")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0242.3 - Valid Anagram - Solution 3 - Sorting """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isAnagram(self, s: str, t: str) -> bool:
"""Valid Anagram Function"""
return sorted(s) == sorted(t)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isAnagram("anagram", "nagaram")) # True
print(Solution().isAnagram("rat", "car")) # False
print(Solution().isAnagram("a", "ab")) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()