-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY 16
More file actions
33 lines (27 loc) · 764 Bytes
/
DAY 16
File metadata and controls
33 lines (27 loc) · 764 Bytes
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
class Solution {
public:
string getSignature(const string& s) {
vector<int> count(26, 0);
for (char c : s) {
count[c - 'a']++;
}
stringstream ss;
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
ss << (char)('a' + i) << count[i];
}
}
return ss.str();
}
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
unordered_map<string, vector<string>> groups;
for (const string& s : strs) {
groups[getSignature(s)].push_back(s);
}
for (const auto& entry : groups) {
result.push_back(entry.second);
}
return result;
}
};