-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_Anagrams.cpp
39 lines (33 loc) · 962 Bytes
/
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
#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include <algorithm>
using namespace std;
int main(){
unordered_map<string, vector<string>> hashMP;
vector<vector<string>> ans;
vector<string> strs = {"eat","tea","tan","ate","nat","bat"};
for(const string text: strs){
string temp = text;
sort(temp.begin(), temp.end());
hashMP[temp].push_back(text);
}
// 遍歷這個 hash map
for (const auto& n : hashMP) {
vector<string> temp;
for(const string& vectorVal : n.second){ // sector 代表 hashmap 的 value
temp.push_back(vectorVal);
}
ans.push_back(temp);
}
// 遍歷這個 hash map
// for (const auto& n : hashMP) {
// cout << "name: " << n.first << " ";
// for(const string& vectorVal : n.second){
// cout << "Value: " << vectorVal << " ";
// }
// cout << "\n";
// }
return 0;
}