Anagrams
两个字符串的字符个数完全相同,这两个字符串是Anagrams。因此Anagrams至少指俩字符串。找出字符集合中的Anagrams组。
vector<string> anagrams(vector<string> &strs)
{
vector<string> ret;
map<string, vector<const string *> > mapAnagram;
for (vector<string>::const_iterator it = strs.begin();
it != strs.end();
++it)
{
string key(*it);
sort(key.begin(), key.end());
mapAnagram[key].push_back(&*it);
}
for (map<string, vector<const string *> >::const_iterator it = mapAnagram.begin();
it != mapAnagram.end();
++it)
{
if (it->second.size() > 1)
{
for (vector<const string *>::const_iterator itstr = it->second.begin();
itstr != it->second.end();
++itstr)
{
ret.push_back(**itstr);
}
}
}
return ret;
}