Had the following as an interview question a while ago and choked so bad on basic syntax that I failed to advance (once the adrenalin kicks in, coding goes out the window.)
Given a list of string, return a list of sets of strings that are anagrams of the input set. i.e. "dog","god", "foo" should return {"dog","god"}. Afterward, I created the code on my own as a sanity check and it's been around now for a bit. I'd welcome input on it to see if I missed anything or if I could have done it muc开发者_如何学编程h more efficiently. Take it as a chance to improve myself and learn other techniques:
void Anagram::doWork(list input, list> &output)
{
typedef list < pair < string, string>> SortType;
SortType sortedInput;
// sort each string and pair it with the original
for(list< string >::iterator i = input.begin(); i != input.end(); ++i)
{
string tempString(*i);
std::sort(tempString.begin(), tempString.end());
sortedInput.push_back(make_pair(*i, tempString));
}
// Now step through the new sorted list
for(SortType::iterator i = sortedInput.begin(); i != sortedInput.end();)
{
set< string > newSet;
// Assume (hope) we have a match and pre-add the first.
newSet.insert(i->first);
// Set the secondary iterator one past the outside to prevent
// matching the original
SortType::iterator j = i;
++j;
while(j != sortedInput.end())
{
if(i->second == j->second)
{
// If the string matches, add it to the set and remove it
// so that future searches need not worry about it
newSet.insert(j->first);
j = sortedInput.erase(j);
}
else
{
// else, next element
++j;
}
}
// If size is bigger than our original push, we have a match
// - save it to the output
if(newSet.size() > 1)
{
output.push_back(newSet);
}
// erase this element and update the iterator
i = sortedInput.erase(i);
}
}
Here's a second pass at this after reviewing comments and learning a bit more:
void doBetterWork(list input, list> &output)
{
typedef std::multimap< string, string > SortedInputType;
SortedInputType sortedInput;
vector< string > sortedNames;
for(vector< string >::iterator i = input.begin(); i != input.end(); ++i)
{
string tempString(*i);
std::sort(tempString.begin(), tempString.end());
sortedInput.insert(make_pair(tempString, *i));
sortedNames.push_back(tempString);
}
for(list< string >::iterator i = sortedNames.begin(); i != sortedNames.end(); ++i)
{
pair< SortedInputType::iterator,SortedInputType::iterator > bounds;
bounds = sortedInput.equal_range(*i);
set< string > newSet;
for(SortedInputType::iterator j = bounds.first; j != bounds.second; ++j)
{
newSet.insert(j->second);
}
if(newSet.size() > 1)
{
output.push_back(newSet);
}
sortedInput.erase(bounds.first, bounds.second);
}
}
The best way to group anagrams is to map the strings to some sort of histogram representation.
FUNCTION histogram
[input] -> [output]
"dog" -> (1xd, 1xg, 1xo)
"god" -> (1xd, 1xg, 1xo)
"foo" -> (1xf, 2xo)
Basically, with a linear scan of a string, you can produce the histogram representation of how many of each letters it contains. A small, finite alphabet makes this even easier (e.g. with A-Z
, you just have an array of 26 numbers, one for each letter).
Now, anagrams are simply words that have the same histogram.
Then you can have a multimap data structure that maps a histogram to a list of words that have that histogram.
MULTIMAP
[key] => [set of values]
(1xd, 1xg, 1xo) => { "dog", "god" }
(1xf, 2xo) => { "foo" }
The canonical form trick
Instead of working on the histograms, you can also work on the "canonical form" of the strings. Basically, you define for each string, what its canonical form is, and two words are anagrams if they have the same canonical form.
One convenient canonical form is to have the letters in the string in sorted order.
FUNCTION canonize
[input] -> [output]
"dog" -> "dgo"
"god" -> "dgo"
"abracadabra" -> "aaaaabbcdrr"
Note that this is just one step after the histogram approach: you're essentially doing counting sort to sort the letters.
This is the most practical solution in actual programming language to your problem.
Complexity
Producing the histogram/canonical form of a word is practically O(1)
(finite alphabet size, finite maximum word length).
With a good hash implementation, get
and put
on the multimap is O(1)
.
You can even have multiple multimaps, one for each word length.
If there are N
words, putting all the words into the multimaps is therefore O(N)
; then outputting each anagram group is simply dumping the values in the multimaps. This too can be done in O(N)
.
This is certainly better than checking if each pair of word are anagrams (an O(N^2)
algorithm).
I'll just treat it as a free function get_anagrams
since class Anagram
doesn't appear to do anything else.
The choice of list
and set
aren't any better than anything else, so while I'm at it I'll make them both vector
. Actually, the output can be just one flat sequence. So call it anagram_sort
. I'll take the specification of "list" and "set" as the basic constructs rather than literal class templates. Also "given … return" can be loosely interpreted as modifying the input in-place. The caller can create a copy if they like.
struct anagram_less { bool operator()( string const &l, string const &r ) {
string sl( l ), sr( r );
sort( sl.begin(), sl.end() );
sort( sr.begin(), sr.end() );
return sl < sr;
} };
void anagram_sort( vector< string > &v ) { // "the answer"
sort( v.begin(), v.end(), anagram_less );
} // 10 lines
// usage:
void print_anagrams( vector< string > &&v ) { // destructive => use rvalue ref
anagram_sort( v );
for ( vector< string >::iterator i = v.begin(); i != v.end(); /*++i*/ ) {
vector< string >::iterator e // find end of run of anagrams
= adjacent_find( i, v.end(), anagram_less() );
if ( e != v.end() ) ++ e; // usually need this after adjacent_find :v(
cout << "( ";
for ( ; i != e; ++ i ) {
cout << * i << " ";
}
cout << ")" << endl;
}
}
This is suboptimal as it repetitively sorts the words. I'd rather cut the fat in an interview question than make something that's fast "on paper," though.
To squeeze that last bit of performance out, I'd probably make a copy of the input with serial-number indexes attached, sort the characters of the strings, sort the strings, and then use the indexes to reorder the input vector using this algorithm. Final runtime a low-coefficient O(n log n), as it should be.
The algorithm to check whether two strings are anagrams of each other goes as follows
convert the two strings in such a way that it only contains letter (because mother-in-law and women hitler is also anagram).Also make the case of two strings equal means either both strings are in upper case or in lower case.
now sort the characters in both strings.
compare the two strings if they are equal means they are anagrams of each other
Here goes the code for this algo
bool checkanagrams(string first,string second)
{
string tempfirst,tempsecond;
int countfirst = 0;
int countsecond = 0;
// only storing characters removing other junk things like -
for(int i=0;i<first.length();i++)
{
if(isalpha(first[i])
{
tempfirst[countfirst] =first[i];
countfirst++;
}
}
for(int i=0;i<second.length();i++)
{
if(isalpha(second[i])
{
tempsecond[countsecond] =second[i];
countsecond++;
}
}
sort(tempfirst.begin(),tempfirst.end());
sort(tempsecond.begin(),tempsecond.end());
if(!strcmp(tempfirst.c_str(),tempsecond.c_str())
return true;
else
return false;
}
I would put everything in a hashtable. Then for each string, I revert it and check if it exists in the hashtable, if so return both the reversed string and itself in a set.
This approach also finds the palindromes in a set.
精彩评论