这个题目大致思路因为字母异位词排序后一样,所以map让这些排序后的字母异位词在同一个vector中,然后遍历map,并把之前相同的字母异位词的vector放入最后的结果中
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs)
{
vector<vector<string>>res;
unordered_map<string, vector<string>>mp;
for (auto it : strs)
{
string key = it;
sort(key.begin(),key.end());
mp[key].push_back(it);
}
for (auto it = mp.begin(); it != mp.end(); it++)
{
res.push_back(it->second);
}
return res;
}
};