0%

49. Group Anagrams

49. Group Anagrams

按照排序后的字符串作为索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> map;
for(auto& str : strs)
{
string key = str;
sort(key.begin(), key.end());
map[key].push_back(str);
}
vector<vector<string>> ret;
for(auto& [_, it] : map)
ret.push_back(std::move(it));
return ret;
}
};
根据字母出现的频次来hash

官解的手写hash函数或者string作为位图,每一位来记录。