For a given pair of words check if they are anagrams. Print
YES
if they are anagrams, or else print
NO
.
Two words are anagrams if one word can be obtained by the rearrangement of the characters in another word. Ex:
knee
and
keen
are anagrams
Note: All characters in the input will be in lower case.
def areAnagram(str1, str2):
n1 = len(str1)
n2 = len(str2)
if n1 != n2:
return 0
str1 = sorted(str1)
str2 = sorted(str2)
for i in range(0, n1):
if str1[i] != str2[i]:
return 0
return 1
str1 = input(print("Enter a string 1 : "))
str2 = input(print("Enter string 2 : "))
if areAnagram(str1, str2):
print("The two strings are anagram of each other")
else:
print("The two strings are not anagram of each other")
Comments
Leave a comment