-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagrams.py
80 lines (53 loc) · 2.01 KB
/
Anagrams.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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
A program to check whether two given strings are anagrams of one another or not.
An anagram is a word (or phrase) that is formed by rearranging the letters of another word (or phrase).
For example:
"rat" is an anagram of "art"
"alert" is an anagram of "alter"
"Slot machines" is an anagram of "Cash lost in me"
"""
"""
check_for_anagram
"""
def check_for_anagram(string_one, string_two):
list_from_string_one = []
list_from_string_two = []
isAnagram = bool
for i in range(len(string_one)):
list_from_string_one.append(string_one[i])
for i in range(len(string_two)):
list_from_string_two.append(string_two[i])
list_from_string_one.sort()
list_from_string_two.sort()
if len(list_from_string_one) != len(list_from_string_two):
print("The words are not anagrams")
return
else:
for i in range(len(list_from_string_one)):
if list_from_string_one[i] != list_from_string_two[i]:
isAnagram = False
else:
continue
if (isAnagram):
print ("The two words are anagrams")
else:
print("The two words are not anagrams")
def check_for_anagrams_optimised(string_one, string_two):
hash_list_for_string_one = [0] * 26
hash_list_for_string_two = [0] * 26
string_one.lower()
string_two.lower()
if len(string_one) != len(string_two):
print("Two words are not anagrams")
return
else:
for i in range(len(string_one)):
index_to_store = ord(string_one[i]) - ord('a')
hash_list_for_string_one[index_to_store] = hash_list_for_string_one[index_to_store] + 1
index_for_second_list = ord(string_two[i]) - ord('a')
hash_list_for_string_two[index_for_second_list] = hash_list_for_string_two[index_for_second_list] + 1
print (hash_list_for_string_one)
print("Hash List")
print (hash_list_for_string_two)
#check_for_anagrams_optimised("alter", "alert")
check_for_anagrams_optimised("Shivam", "Anirud")