49. 字母异位词分组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > ans;
map<string, vector<string> > M;
for(auto str : strs) {
string temp = str;
sort(str.begin(), str.end());
if(M.find(str) == M.end()) M[str] = {temp};
else M[str].push_back(temp);
}
for(auto it = M.begin(); it != M.end(); it++) {
ans.push_back(it->second);
}
return ans;
}
};
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > ans;
map<vector<int>, vector<string> > M;
for(auto str : strs) {
vector<int> V(26, 0);
for(auto ch : str) V[ch - 'a']++;
if(M.find(V) == M.end()) M[V] = {str};
else M[V].push_back(str);
}
for(auto it = M.begin(); it != M.end(); it++) {
ans.push_back(it->second);
}
return ans;
}
};