|
ADS
Algorithms and Data Structures
|
The classes of anagrams among \( n \) words of length at most \( k \): two words are anagrams if one is a permutation of the other. More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <functional>#include <string>#include <unordered_map>#include <utility>#include <vector>#include "ADS/string/impl/Anagrams.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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) | |
The classes of anagrams among \( n \) words of length at most \( k \): two words are anagrams if one is a permutation of the other.
anagramGroups() gives the signature of each word, its items sorted, and gathers the words of equal signature in a hash table, \( O( n k \log k ) \) expected time and \( O( n k ) \) space; the groups come in the order of their first word, and the words of a group in their order. The signature by counting sort over an alphabet of \( \sigma \) symbols, or the vector of the counts of the symbols, costs \( O( k + \sigma ) \) per word instead; sorting the pairs \( ( \mathit{signature} , \mathit{word} ) \) in place of hashing them gives the classes in \( O( n k \log n ) \) worst-case time.
anagramGroups() takes the words as any sequence S with an iterator constructor, sortable items and a hash, std::hash< S > by default; the overload on std::string also takes a list of literals.
The contracts [see Contract.h]: the groups partition the words, and the words of a group are anagrams of its first (an audit). The functions are defined in Anagrams.tpp.