Answer to Question #208465 in Python for PARTHU

Question #208465

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.


1
Expert's answer
2021-06-18T12:33:53-0400
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")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS