-
Notifications
You must be signed in to change notification settings - Fork 28
/
Group_Anagrams.cpp
35 lines (31 loc) · 1.03 KB
/
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
class Solution {
string countingSort(string str) {
int count[30] = {0};
for(int j = 0; j < str.length(); ++j) {
count[(int)(str[j] - 'a')]++;
}
string sortedStr;
int indx = 0, idx = 0;
for(char c = 'a'; c <= 'z'; ++c, ++indx) {
while(count[indx]--)
sortedStr.push_back(c);
}
return sortedStr;
}
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
if(strs.empty()) return result;
unordered_map<string, vector<string>> anagramMap;
for(string& str : strs) {
// string sortedStr = str;
// sort(sortedStr.begin(), sortedStr.end());
string sortedStr = countingSort(str);
anagramMap[sortedStr].push_back(str);
}
for(auto it = anagramMap.begin(); it != anagramMap.end(); ++it) {
result.push_back(it->second);
}
return result;
}
};