-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupAnagrams_49.cpp
More file actions
34 lines (29 loc) · 852 Bytes
/
groupAnagrams_49.cpp
File metadata and controls
34 lines (29 loc) · 852 Bytes
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
#include<vector>
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> hash;
for(int i = 0; i < strs.size(); ++i){
string tmp = strs[i];
sort(tmp.begin(), tmp.end());
if(hash.count(tmp)){
hash[tmp].push_back(strs[i]);
}else{
hash.insert(pair<string, vector<string>>(tmp, vector<string>()));
//hash.emplace((tmp, vector<string>()));
hash[tmp].push_back(strs[i]);
}
}
for(auto ite : hash){
res.emplace_back(std::move(ite.second));
}
return res;
}
int main(){
vector<int> vec {2,1,3,4};
sort(vec.begin(), vec.end(), std::less<int>());
return 0;
}