Question d’entretien chez Capital One

Code the algorithm for figuring out an anagram between two strings

Réponse à la question d'entretien

Utilisateur anonyme

24 févr. 2020

def areAnagram(str1, str2): # Get lengths of both strings a1 = len(str1) a2 = len(str2) # If length of both strings is not same, then # they cannot be an anagram if a1 != a2: return 0 # Sort both strings str1 = sorted(str1) str2 = sorted(str2) # Compare sorted strings for i in range(0, a1): if str1[i] != str2[i]: return 0 return 1 # Driver program to test the above function str1 = "test" str2 = "test1" if areAnagram(str1, str2): print ("The two strings are anagram of each other") else: print ("The two strings are not anagram of each other")