-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49.cc
39 lines (30 loc) · 801 Bytes
/
49.cc
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
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string> &strs) {
std::unordered_map<std::string, std::vector<std::string>> m;
for (const auto &s : strs) {
auto sorted = s;
std::sort(sorted.begin(), sorted.end());
m[sorted].push_back(s);
}
std::vector<std::vector<std::string>> res;
for (auto it = m.begin(); it != m.end(); ++it) {
res.push_back(it->second);
}
return res;
}
};
int main() {
Solution s;
std::vector<std::string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
auto res = s.groupAnagrams(strs);
strs = {""};
res = s.groupAnagrams(strs);
strs = {"a"};
res = s.groupAnagrams(strs);
}