forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_anagram.py
33 lines (25 loc) · 1000 Bytes
/
valid_anagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'''
we are going to use a hashmap approach to count the number of characters in both the strings and
finally compare both hashmaps.
'''
from collections import defaultdict
class valid_anagram:
#method to determine if two strings are valid anagrams.
def isAnagram(self, s: str, t: str) -> bool:
#initialize two dictionaries to be used as hashmaps.
dictis=defaultdict()
dictit=defaultdict()
#count the occurences of each character in both the strings.
for i in s:
dictis[i]=dictis.get(i,0)+1
for i in t:
dictit[i]=dictit.get(i,0)+1
#if both the dictionaries are the same, return True
return True if dictis==dictit else False
#example usage
if __name__=='__main__':
#create an instance of the valid_anagram class.
anagram_checker=valid_anagram()
#call the isAnagram method
result = anagram_checker.isAnagram("dusty","study")
print(result)