-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-49-Group_Anagrams.cpp
54 lines (45 loc) · 1.34 KB
/
leetcode-49-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
52
53
54
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
class Solution {
public:
std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string>& strs) {
std::unordered_map<std::string, size_t> patterns_place;
std::vector<std::vector<std::string>> res;
for(const auto & s : strs)
{
std::string temp_s = s;
std::sort(temp_s.begin(), temp_s.end());
if(patterns_place.find(temp_s)==patterns_place.end()) {
patterns_place[temp_s] = res.size();
res.emplace_back(std::vector<std::string>{s});
} else {
res[patterns_place[temp_s]].emplace_back(s);
}
}
return res;
}
};
int main(){
std::vector<std::string> strs = {"eat","tea","tan","ate","nat","bat"};
auto res = Solution().groupAnagrams(strs);
std::cout << "[";
for(size_t i = 0; i < res.size(); i++)
{
std::cout << "[";
for(size_t j = 0; j < res[i].size(); j++)
{
std::cout << res[i][j];
if(j!=res[i].size()-1)
std::cout << ", ";
}
std::cout << "]";
if(i!=res.size()-1)
std::cout << ", ";
}
std::cout << "]" << std::endl;
return EXIT_SUCCESS;
}