-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.py
More file actions
58 lines (44 loc) · 1.14 KB
/
anagram.py
File metadata and controls
58 lines (44 loc) · 1.14 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
Check if s1 and s2 are anagram
'''
def hashmap(s1, s2):
#check if s1 and s2 has the same length
#if no, then output false
#if yes, then compare the string -> loop or hashmap
if(len(s1) != len(s2)):
return False
map1 = {}
map2 = {}
for ch1 in s1:
if ch1 in map1:
map1[ch1] += 1
else:
map1[ch1] = 1
for ch2 in s2:
if ch2 in map2:
map2[ch2] += 1
else:
map2[ch2] = 1
for key in map1:
if key not in map2 or map1[key] != map2[key]:
return False
return True
def sorting(s1, s2):
if(len(s1) != len(s2)):
return False
return sorted(s1) == sorted(s2)
def main():
s1 = "nameless"
s2 = "salesmen"
hashmap_value = hashmap(s1,s2)
if hashmap_value == True:
print("Hashmap method shows Anagram")
else:
print("Hashmap method shows Not Anagram")
sorting_value = sorting(s1, s2)
if sorting_value == True:
print("Sorting method shows Anagram")
else:
print("Sorting method shows Not Anagram")
if __name__ == "__main__":
main()