Wolfram Research

Function Repository Resource:

AnagramQ

Source Notebook

Test whether strings are anagrams

Contributed by: Sander Huisman

ResourceFunction["AnagramQ"][s1,s2]

yields True if s1 is an anagram of s2.

ResourceFunction["AnagramQ"][s1,s2,]

yields True if all the strings si are anagrams of one another.

Details and Options

By default, spaces and the case of the letters are ignored.
ResourceFunction["AnagramQ"] has the following options:
IgnoreCase True treats lowercase and uppercase letters as equivalent.
IgnoreDiacritics False treats diacritics and regular letters as equivalent.
"IgnoreSpaces" True ignores spaces from the strings.
The option IgnoreDiacritics True effectively applies RemoveDiacritics to each string before the comparison. The outcome of RemoveDiacritics might depend on the $Language setting.
ResourceFunction["AnagramQ"] has an arbitary arity.
Like Equal and SameQ, ResourceFunction["AnagramQ"] without any arguments (i.e. ResourceFunction["AnagramQ"][]) yields True.
Like Equal and SameQ, ResourceFunction["AnagramQ"] with a single argument (i.e. ResourceFunction["AnagramQ"][s1]) yields True.
ResourceFunction["AnagramQ"] with more than two arguments will check if all the arguments are anagrams of each other.

Examples

Basic Examples

Test whether “trace” is an anagram of “crate”:

In[1]:=
ResourceFunction["AnagramQ"]["trace", "crate"]
Out[1]=

Test whether “the Morse code” is an anagram of “Here come dots”:

In[2]:=
ResourceFunction["AnagramQ"]["the Morse code", "Here come dots"]
Out[2]=

Scope

AnagramQ accepts more than two arguments and will check if all of them are anagrams of one another:

In[3]:=
ResourceFunction["AnagramQ"]["trace", "crate", "caret"]
Out[3]=

Options

Use the option IgnoreCase to ignore the case of the letters:

In[4]:=
ResourceFunction["AnagramQ"]["Elvis", "lives", IgnoreCase -> False]
Out[4]=
In[5]:=
ResourceFunction["AnagramQ"]["Madam Curie", "radium came", IgnoreCase -> True]
Out[5]=

Use the option IgnoreDiacritics to ignore diacritics:

In[6]:=
ResourceFunction["AnagramQ"]["diamanté", "animated", IgnoreDiacritics -> True]
Out[6]=
In[7]:=
ResourceFunction["AnagramQ"]["diamanté", "animated", IgnoreDiacritics -> False]
Out[7]=

Use the option "IgnoreSpaces" to decide whether or not to ignore spaces:

In[8]:=
ResourceFunction["AnagramQ"]["schoolmaster", "the classroom", "IgnoreSpaces" -> True]
Out[8]=
In[9]:=
ResourceFunction["AnagramQ"]["schoolmaster", "the classroom", "IgnoreSpaces" -> False]
Out[9]=

Applications

Look for anagrams of a word using the built-in dictionary:

In[10]:=
Select[DictionaryLookup[], ResourceFunction["AnagramQ"][#, "sander", IgnoreCase -> False] &]
Out[10]=

Test whether two sentences are anagrams of one another:

In[11]:=
ResourceFunction["AnagramQ"]["eleven plus two", "twelve plus one"]
Out[11]=

Possible Issues

Two equal strings are not considered anagrams:

In[12]:=
ResourceFunction["AnagramQ"]["hello", "hello"]
Out[12]=

Two empty strings are also not considered anagrams of one another:

In[13]:=
ResourceFunction["AnagramQ"]["", ""]
Out[13]=

AnagramQ returns True if a single argument is supplied:

In[14]:=
ResourceFunction["AnagramQ"]["Just a single argument"]
Out[14]=

AnagramQ returns True if no arguments are supplied:

In[15]:=
ResourceFunction["AnagramQ"][]
Out[15]=

Depending on $Language the option IgnoreDiacritics can affect the outcome:

In[16]:=
Block[{$Language = "Swedish"},
 ResourceFunction["AnagramQ"]["malmö", "amolm", IgnoreDiacritics -> True]
 ]
Out[16]=
In[17]:=
Block[{$Language = "English"},
 ResourceFunction["AnagramQ"]["malmö", "amolm", IgnoreDiacritics -> True]
 ]
Out[17]=

Neat Examples

Find anagrams with six or more words with at least five letters:

In[18]:=
words = Select[DictionaryLookup[], GreaterEqualThan[5]@*StringLength];
words = DeleteDuplicates[ToLowerCase[words]];
words = Function[{x}, {x, Sort[Characters[x]]}] /@ words;
words = GatherBy[words, Last];
anagrams = Select[words, GreaterEqualThan[6]@*Length];
anagrams = anagrams[[All, All, 1]];
ReverseSortBy[anagrams, Length] // Grid
Out[24]=

Confirm that all the groups of words are anagrams:

In[25]:=
ResourceFunction["AnagramQ"] @@@ anagrams
Out[25]=

Resource History

License Information