题目:字母异位词分组
实现思路:记录每个字符串中单个字母的出现频率,并以字符串形式存储起来(如”0110200000…."),可以提前将字符串的空间预留26位(对应26个字母),然后将这个字符串作为哈希表的key,用来查找表中存储着异位词的vector值
1vector<vector<string>> groupAnagrams(vector<string>& strs)
2{
3 std::vector<vector<string>> res;
4 std::unordered_map<string, std::vector<string>> um_pair;
5 for(auto& str : strs)
6 {
7 string count(26, 0);
8 for(auto& c : str)
9 {
10 count[c - 'a'] += 1;
11 }
12 um_pair[count].push_back(str);
13 }
14 for(auto& pair : um_pair)
15 {
16 res.push_back(pair.second);
17 }
18 return res;
19}