-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution49.cpp
More file actions
34 lines (32 loc) · 877 Bytes
/
Solution49.cpp
File metadata and controls
34 lines (32 loc) · 877 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
//
// Solution49.cpp
// Algorithm
//
// Created by Pancf on 2020/10/25.
// Copyright © 2020 Pancf. All rights reserved.
//
#include "Solution49.hpp"
#include <algorithm>
#include <map>
std::vector<std::vector<std::string>> Solution49::groupAnagrams(std::vector<std::string> &strs)
{
std::vector<std::vector<std::string>> res;
if (strs.size() == 0) return res;
std::map<std::string, std::vector<std::string>> dict;
for (auto str : strs) {
auto copyStr{str};
std::sort(copyStr.begin(), copyStr.end());
if (dict.find(copyStr) == dict.end()) {
// not found
std::vector<std::string> v{str};
dict[copyStr] = v;
} else {
auto& v = dict[copyStr];
v.push_back(str);
}
}
for (auto pair : dict) {
res.push_back(pair.second);
}
return res;
}