Python program to check anagram string.

An anagram of a string is another string that contains the same characters, only the order of characters can be different.

For example, “abcd” and “dabc” are an anagram of each other.

Also it should not contain characters at same location, for eg. "listen" & "silent" have "i" at same location. So it is not an anagram string.

Here is the code:

                

def check_anagram(data1,data2):
    x=data1.lower()
    y=data2.lower()
    count=0
    for i in range(0,len(data1)):
        if x[i]!=y[i]:
            count+=1
    if count==len(data1) and sorted(x)==sorted(y):
        return True
    else:
        return False    

print(check_anagram("silent","listen"))

Comments

  1. Is it have order same or different ?

    ReplyDelete
    Replies
    1. It depends on problem statement. In some cases it is not necessary but in some cases order of element must be different.

      Delete

Post a Comment

Popular Posts