Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:
All inputs will be in lowercase.
The order of your output does not matter.
这道题可以将单词排序作为字典的键,然后取出字典的值即可。
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> strMap;
for(auto str : strs){
string temp = str;
sort(temp.begin(), temp.end());
strMap[temp].push_back(str);
}
for(auto group : strMap){
res.push_back(group.second);
}
return res;
}
};