Skip to content

Instantly share code, notes, and snippets.

@marius92mc
Created January 8, 2015 12:24
Show Gist options
  • Save marius92mc/8de46e3b460742dfd1e3 to your computer and use it in GitHub Desktop.
Save marius92mc/8de46e3b460742dfd1e3 to your computer and use it in GitHub Desktop.
vector<string> anagrams(vector<string> &strs)
{
int strs_size = strs.size();
vector<string> result;
map<string, vector<string> > m;
for (int i = 0; i < strs_size; i++)
{
string temp = strs[i];
sort(temp.begin(), temp.end());
m[temp].push_back(strs[i]);
}
for (auto it = m.begin(); it != m.end(); it++)
{
vector<string> words = it->second;
int count_words = words.size();
for (int j = 0; j < count_words; j++)
result.push_back(words[j]);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment