I want to write a function that takes 2 strings and returns True if they are anagrams(any word that have the same letters in different order) [duplicate]

Consider using collections.counter:

import collections

s1 = "Army"
s2 = "Mary"
s3 = "Zary"

def anagram(s1,s2):
  return collections.Counter(s1.lower()) == collections.Counter(s2.lower())

print anagram(s1,s2) # True
print anagram(s1, s3) # False

Try it here!

Browse More Popular Posts

Leave a Comment