-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.py
More file actions
53 lines (42 loc) · 1.39 KB
/
anagram.py
File metadata and controls
53 lines (42 loc) · 1.39 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
def repeatcheck(stringtocheck):
retval = None
letterdict = {}
strlen = len(stringtocheck)
print strlen
hasrepeatingletters = False
for i in range(0, strlen):
if(letterdict.has_key(stringtocheck[i])):
print '%s already in dictionary' % stringtocheck[i]
letterdict[stringtocheck[i]] = stringtocheck[i]
print letterdict
def isAnagram(string1, string2):
"""
Two strings are anagrams if they have the same letters in any order.
They should have the same length
"""
if not string1 or not string2:
return 'Both strings must be specified'
string1length = len(string1)
string2length = len(string2)
string2 = string2.lower()
if string1 == string2:
print "These strings are the same. They are not anagrams"
elif string1length == string2length:
#the string lengths are the same
found_string = True
for i in range(0, string1length):
charac = string1[i].lower()
if string2.find(charac) == -1:
found_string = False
break
if(found_string):
print 'The strings are anagrams'
else:
print 'The strings are not anagrams'
else:
print "The strings are not anagrams"
if(__name__=="__main__"):
#Anagrams
isAnagram('Raji', 'jira')
#not anagrams
isAnagram('haha', 'jira')