计数
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>res;
unordered_map<string,vector<string>>hash_al; //unordered_map的key不能存储vector.
for(auto &t:strs){
string tCount(26,'0');
for(char c:t){
--tCount[c-'a'];
}
hash_al[tCount].emplace_back(t);
}
for(auto it=hash_al.begin();it!=hash_al.end();++it){
res.push_back(it->second);
}
return res;
}
};
排序
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>>res;
unordered_map<string,vector<string>>hash_al;
for(auto &t:strs){
string temp = t;
sort(temp.begin(),temp.end());
hash_al[temp].emplace_back(t);
}
for(auto it=hash_al.begin();it!=hash_al.end();++it){
res.push_back(it->second);
}
return res;
}
};