-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
55 lines (48 loc) · 1.13 KB
/
main.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<string>> groupStrings(vector<string>& strings) {
unordered_map<string, multiset<string>> groups;
for (const auto& str : strings) {
groups[hashStr(str)].insert(str);
}
vector<vector<string>> result;
for (const auto& kvp : groups) {
vector<string> group;
for (auto& str : kvp.second) {
group.emplace_back(move(str));
}
result.emplace_back(move(group));
}
return result;
}
string hashStr(string str) {
const char base = str[0];
for (auto& c : str) {
c = 'a' + ((c - base) >= 0 ? c - base : c - base + 26);
}
return str;
}
};
int main() {
Solution sol;
vector<string> strs = {
"abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"
};
vector<vector<string>> result = sol.groupStrings(strs);
for (auto vs : result) {
cout << "[ ";
for (auto s : vs) {
cout << s << " ";
}
cout << "]" << endl;
}
return 0;
}