Write a program to find whether the 2 given strings are anagrams or not.
Anagrams are words or phrases made by mixing up the letters of other words or
phrases,
Input and Output Format:
Input consists of 2 string.
Assume that all characters in the string are lowercase letters or spaces and
the maximum length of the string is 100. Refer sample input and output for
formatting specifications. All text in bold corresponds to input and the rest
corresponds to output.
Sample Input and Output 1:
Enter the
first string
anitha
anitha
Enter the second string
amphisoft
amphisoft
anitha and amphisoft are
not anagrams
Sample Input and Output 2:
Enter the first
string
the eyes
the eyes
Enter the second string
they see
they see
the eyes and they
see are anagrams
Solution:
Method – 1:
print('Enter the first string')
str1=input()
print('Enter the second string')
str2=input()
from collections import Counter
if Counter(str1)== Counter(str2):
print(str1+' and '+str2+'
are anagrams')
else:
print(str1+' and '+str2+'
are not anagrams')
Method – 2:
def anagram(s1,s2):
if len(s1)
is len(s2):
count1=[0]*25
count2=[0]*25
for i in
range(len(s1)):
count1[ord(s1[i])-ord('a')]+=1
count2[ord(s2[i])-ord('a')]+=1
for i in
range(25):
if
count1[i] is not count2[i]:
return 0
return 1
else:
return 0
print('Enter the first string')
str1=input()
print('Enter the second string')
str2=input()
if anagram(str1,str2):
print(str1+' and '+str2+' are anagrams')
else:
print(str1 + ' and ' + str2 + ' are not anagrams')