-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0049_Group_Anagrams.cpp
51 lines (41 loc) · 1.25 KB
/
0049_Group_Anagrams.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
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > output;
unordered_map<string, vector<string> > key_strs;
unordered_map<string, vector<string> >::iterator it;
// save as map
for(int i = 0; i < strs.size(); i++){
// sort each words
string s = strs[i];
sort(s.begin(), s.end());
key_strs[s].push_back(strs[i]);
}
// save the result based on map
for(it = key_strs.begin(); it != key_strs.end(); it++)
output.push_back(it->second);
return output;
}
};
int main(){
Solution solve;
vector<string> input = {"eat", "tea", "tan", "ate", "nat", "bat"};
cout << "Input: " << endl;
for(int i = 0; i < input.size(); i++) cout << input[i] << " ";
cout << endl;
vector<vector<string> > output = solve.groupAnagrams(input);
cout << "Output:" << endl;
for(int i = 0; i < output.size(); i++){
for(int j = 0; j < output[i].size(); j++){
cout << output[i][j] << " ";
}
cout << endl;
}
return 0;
}