detecting anagrams in python

what’s the shortest anagram detector you know how to write, in python?


this is one that keeps feeling like it should be a one liner (because it’s very similar to this one liner), but i suspect can’t be done in one line

highlight to see a solution.


def fn(x,y):
    if len(x) != len(y):
        return False
    for i in x:
        y = y.replace(i,'',1)
    return not len(y) > 0

PS: there’s a cool method for solving this for a fixed alphabet, using primes


[More programming riddles]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s