Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
Created April 14, 2010 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quandyfactory/366041 to your computer and use it in GitHub Desktop.
Save quandyfactory/366041 to your computer and use it in GitHub Desktop.
Determine whether two strings are anagrams.
def anagram(string1, string2, ignore_whitespace=False):
"""Determines whether two strings are anagrams."""
if ignore_whitespace==True:
import re
string1, string2 = re.sub('\s', '', string1), re.sub('\s', '', string2)
if len(string1) != len(string2): return False
list1, list2 = [c for c in string1].sort(), [c for c in string2].sort()
if list1 != list2: return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment