Given an array of strings, group anagrams together.
Example:
Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],
Output:
[
[“ate”,“eat”,“tea”],
[“nat”,“tan”],
[“bat”]
]
C++
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string,vector<string>> m;
for(int i = 0;i < strs.size();++i)
{
string str = strs[i];
std::sort(str.begin(),str.end());
m[str].push_back(strs[i]);
}
for(auto it = m.begin();it != m.end();++it)
{
res.push_back(it->second);
}
return res;
}