anagram python code example
Example 1: python anagrams
# function to check if two strings are
# anagram or not
def check(s1, s2):
# the sorted strings are checked
if(sorted(s1)== sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
# driver code
s1 ="listen"
s2 ="silent"
check(s1, s2)
Example 2: anagram python
def isAnagram(A,B):
if sorted(A) == sorted(B):
print("Yes")
else:
print("No")
isAnagram("earth","heart") #Output: Yes
#Hope this helps:)
Example 3: check if two strings are anagrams python
if sorted(s1) == sorted(s2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
Example 4: anagram
#take user input
String1 = input('Enter the 1st string :')
String2 = input('Enter the 2nd string :')
#check if length matches
if len(String1) != len(String2):
#if False
print('Strings are not anagram')
else:
#sorted function sort string by characters
String1 = sorted(String1)
String2 = sorted(String2)
#check if now strings matches
if String1 == String2:
#if True
print('Strings are anagram')
else:
print('Strings are not anagram')