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:
IgnoreCaseTruetreats lowercase and uppercase letters as equivalent.
IgnoreDiacriticsFalsetreats diacritics and regular letters as equivalent.
"IgnoreSpaces"Trueignores 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 (3) 

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]=

Test whether "extraterrestrial" is an anagram of "xylophone":

In[3]:=
ResourceFunction["AnagramQ"]["extraterrestrial", "xylophone"]
Out[3]=

Scope (1) 

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

In[4]:=
ResourceFunction["AnagramQ"]["trace", "crate", "caret"]
Out[4]=
In[5]:=
ResourceFunction["AnagramQ"]["trace", "crate", "carat"]
Out[5]=

Options (3) 

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

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

Use the option IgnoreDiacritics to ignore diacritics:

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

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

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

Applications (2) 

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

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

Test whether two sentences are anagrams of one another:

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

Possible Issues (5) 

Two equal strings are not considered anagrams:

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

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

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

AnagramQ returns True if a single argument is supplied:

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

AnagramQ returns True if no arguments are supplied:

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

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

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

Neat Examples (2) 

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

In[20]:=
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[26]=

Confirm that all the groups of words are anagrams:

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

Publisher

SHuisman

Version History

  • 1.0.0 – 31 May 2019

License Information