Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions Group Anagrams.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
struct RetrieveValue
{
template <typename T>
typename T::second_type operator()(T keyValuePair) const
{
return keyValuePair.second;
}
};
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>

using namespace std;

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> groups;
for(string& str : strs){

for (const string& str : strs) {
vector<int> count(26, 0);
for(char c : str){
count[c-'a']++;
for (char c : str) {
count[c - 'a']++;
}

string scount = "";
for(int& e : count){
scount += (to_string(e)+"#");

// Create a unique key from the count array
string key;
for (int e : count) {
key += to_string(e) + '#';
}
groups[scount].push_back(str);

groups[key].push_back(str);
}


// Reserve space for performance
vector<vector<string>> ans;
ans.reserve(groups.size());

transform(groups.begin(), groups.end(), back_inserter(ans), RetrieveValue());

for (auto& entry : groups) {
ans.push_back(move(entry.second)); // Move to avoid copying
}

return ans;
}
};