forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0049.cpp
38 lines (36 loc) · 879 Bytes
/
0049.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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}();
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
unordered_map<string, vector<string> > group;
for (auto& s : strs)
{
string key = s;
sort(key.begin(), key.end());
group[key].push_back(s);
}
vector<vector<string>> res;
for (auto& m : group) res.push_back(m.second);
return res;
}
};
int main()
{
vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
for (auto& i : Solution().groupAnagrams(strs))
{
for (auto& j : i)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}