-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path49.cpp
More file actions
26 lines (26 loc) · 800 Bytes
/
Copy path49.cpp
File metadata and controls
26 lines (26 loc) · 800 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
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string> &strs) {
unordered_map<string, vector<string>> res;
for (string &str:strs) {
vector<int> count(26, 0);
for (char s:str)
++count[s - 'a'];
string key;
for (int i = 0; i < 26; ++i) {
key += '#';
key += to_string(count[i]);
}
if (res.find(key) == res.end())
res.insert(pair<string, vector<string>>(key, vector<string>()));
res[key].push_back(str);
}
vector<vector<string>> ret(res.size(), vector<string>());
int i = 0;
for (auto &r:res) {
ret[i] = r.second;
++i;
}
return ret;
}
};