ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Anagrams.h

The classes of anagrams among \( n \) words of length at most \( k \). More...

Files

file  ADS/string/Anagrams.h
 The classes of anagrams among \( n \) words of length at most \( k \): two words are anagrams if one is a permutation of the other.

Functions

template<class S, class Hash = std::hash< S >>
std::vector< std::vector< S > > ADS::anagramGroups (const std::vector< S > &words)
 the classes of anagrams among the words, in the order of their first word, each in the order of the words
std::vector< std::vector< std::string > > ADS::anagramGroups (const std::vector< std::string > &words)
 the classes of anagramGroups() above, on std::string (or a list of literals)

Detailed description

The classes of anagrams among \( n \) words of length at most \( k \).

The definitions, the invariants and the costs of the area are in Theory of strings.

Function documentation

◆ anagramGroups() [1/2]

template<class S, class Hash = std::hash< S >>
std::vector< std::vector< S > > ADS::anagramGroups ( const std::vector< S > & words)
nodiscard

the classes of anagrams among the words, in the order of their first word, each in the order of the words

In pseudocode (chapter 13 and Bentley's column 2), \( H \) a hash table from a signature to the number of its group:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{anagramGroups}( W )\quad \triangleright \ \text{two anagrams share the sorted word} \cr 2 & \quad H \gets \text{an empty table from a key to a group};\ G \gets () \cr 3 & \quad \textbf{foreach}\ w \in W \text{, in order}\ \textbf{do} \cr 4 & \qquad \kappa \gets \text{the letters of } w \text{, sorted} \cr 5 & \qquad \textbf{if}\ H \text{ has no group for } \kappa \ \textbf{then}\ \text{open a new last group of } G \text{ and record it in } H[ \kappa ] \cr 6 & \qquad \text{append } w \text{ to the group } G[ H[ \kappa ] ] \cr 7 & \quad \textbf{return}\ G \end{array}} \]

Postcondition
the groups partition the words, each one of anagrams (audit)

◆ anagramGroups() [2/2]

std::vector< std::vector< std::string > > ADS::anagramGroups ( const std::vector< std::string > & words)
inlinenodiscard

the classes of anagramGroups() above, on std::string (or a list of literals)