Can someone please tell me why my code for Checking if two strings are anagram doesnt work

64 Views Asked by At

This is my code :

string_1 = input("Enter a string : ")
string_2 = input("Enter a second string : ")
a = sorted(string_1)
b = sorted(string_2)
print(a)
print(b)
if a in b and len(a) == len(b):
    print("these strings are anagram")
else:
    print("these strings are not anagram")  

For example :
Input:
Enter a string : mounir
Enter a second string : mounir
Output :

['i', 'm', 'n', 'o', 'r', 'u']
['i', 'm', 'n', 'o', 'r', 'u']
these strings are not anagram

1

There are 1 best solutions below

0
vid cat On

you should try with this one. I changed the condition.

string_1 = input("Enter a string : ")
string_2 = input("Enter a second string : ")
a = sorted(string_1)
b = sorted(string_2)
print(a)
print(b)
if sorted(a) == sorted(b):
print("these strings are anagram")
else:
    print("these strings are not anagram")