forked from cpselvis/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution049.cpp
More file actions
57 lines (50 loc) · 1.07 KB
/
solution049.cpp
File metadata and controls
57 lines (50 loc) · 1.07 KB
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
56
57
/**
* Group Anagrams
* HashTable
*
* cpselvis([email protected])
* September 12th, 2016
*/
#include<iostream>
#include<map>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& strs) {
multimap<string, string> mmap;
vector<vector<string> > ret;
for (auto str : strs)
{
string s = str;
sort(s.begin(), s.end());
mmap.insert(pair<string, string>(s, str));
}
for (multimap<string, string>::iterator iter = mmap.begin(); iter != mmap.end();)
{
int count = mmap.count(iter -> first);
vector<string> tmp;
for (int i = 0; i < count; i ++, iter ++)
{
tmp.push_back(iter -> second);
}
ret.push_back(tmp);
}
return ret;
}
};
int main(int argc, char **argv)
{
string arr[6] = {"eat", "tea", "tan", "ate", "nat", "bat"};
vector<string> strs(arr + 0, arr + 6);
Solution s;
vector<vector<string> > ret = s.groupAnagrams(strs);
for (auto i : ret)
{
for (auto j : i)
{
cout << j << " ";
}
cout << endl;
}
}