-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroupAnagrams3.cpp
More file actions
62 lines (43 loc) · 1.08 KB
/
GroupAnagrams3.cpp
File metadata and controls
62 lines (43 loc) · 1.08 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
58
59
60
61
62
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& strs) {
vector<vector<string> > result;
map<string,vector<string> > strmap;
for(int i=0;i<strs.size();i++){
string where="00000000000000000000000000";
for(int j=0;j<strs[i].length();j++){
where[strs[i][j]-'a']++;
}
strmap[where].push_back(strs[i]);
}
map<string,vector<string> >::iterator it;
for(it=strmap.begin();it!=strmap.end();it++){
result.push_back(it->second);
}
return result;
}
};
int main(){
int n;
cin>>n;
vector<string> strs;
for(int i=0;i<n;i++){
string str;
cin>>str;
strs.push_back(str);
}
Solution *solution=new Solution();
vector<vector<string> > result=solution->groupAnagrams(strs);
for(int i=0;i<result.size();i++){
for(int j=0;j<result[i].size();j++){
cout<<result[i][j]<<", ";
}
cout<<endl;
}
return 0;
}