forked from sachinsingh3232/Hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup Anagrams.cpp
35 lines (28 loc) · 851 Bytes
/
Group Anagrams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
struct RetrieveValue
{
template <typename T>
typename T::second_type operator()(T keyValuePair) const
{
return keyValuePair.second;
}
};
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> groups;
for(string& str : strs){
vector<int> count(26, 0);
for(char c : str){
count[c-'a']++;
}
string scount = "";
for(int& e : count){
scount += (to_string(e)+"#");
}
groups[scount].push_back(str);
}
vector<vector<string>> ans;
transform(groups.begin(), groups.end(), back_inserter(ans), RetrieveValue());
return ans;
}
};