-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49.字母异位词分组.cpp
39 lines (38 loc) · 908 Bytes
/
49.字母异位词分组.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
/*
* @lc app=leetcode.cn id=49 lang=cpp
*
* [49] 字母异位词分组
*/
#include <bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string> &strs)
{
auto arrayHash = [fn = hash<int>{}](const array<int, 26> &arr) -> size_t
{
return accumulate(arr.begin(), arr.end(), 0u, [&](size_t acc, int num)
{ return (acc << 1) ^ fn(num); });
};
unordered_map<array<int, 26>, vector<string>, decltype(arrayHash)> mp(0, arrayHash);
for (auto &&str : strs)
{
array<int, 26> count{};
for (auto &&c : str)
{
count[c - 'a']++;
}
mp[count].emplace_back(str);
}
vector<vector<string>> ans;
for (auto it = mp.begin(); it != mp.end(); it++)
{
/* code */
ans.push_back(it->second);
}
return ans;
}
};
// @lc code=end