StringTools
Anagrams
search for anagrams
Calling Sequence
Parameters
Description
Examples
Anagrams( word, wordlist )
word
-
Maple string; word to anagram
wordlist
sorted list of strings; list of words to search for anagrams
An anagram of a word (string) is a permutation of word. The Anagrams(word, wordlist) command searches wordlist for anagrams of word. An expression sequence of the anagrams of word is returned. The order of the strings in the returned expression sequence is not defined. If no anagrams are found, then the NULL expression sequence is returned.
The list wordlist is required to be a (lexicographically) sorted list of strings. (The requirement that the list be sorted is not currently used or enforced, but may be in a future implementation.)
You can generate the set of all anagrams of a string by using StringTools[Permute], together with combinat[permute], as illustrated in the following example.
All of the StringTools package commands treat strings as (null-terminated) sequences of 8-bit (ASCII) characters. Thus, there is no support for multibyte character encodings, such as unicode encodings.
with⁡StringTools:
Anagrams⁡edit,foo,diet,bar,tide,baz,edit,dite,foobar
tide,diet,edit,dite
Anagrams⁡flower,foo,diet,bar,tide,baz,edit,dite,foobar
To generate all anagrams of a string, use Permute and combinat[permute].
AllAnagrams≔s::string↦op⁡map2⁡StringTools:−Permute,s,combinat:−permute⁡length⁡s
L≔AllAnagrams⁡edit
L≔edit,edti,eidt,eitd,etdi,etid,deit,deti,diet,dite,dtei,dtie,iedt,ietd,idet,idte,ited,itde,tedi,teid,tdei,tdie,tied,tide
evalb⁡nops⁡L=4!
true
See Also
combinat[permute]
convert
length
map
string
StringTools[Permute]
Download Help Document