Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
class Solution {
public:
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 t=strs[i];
sort(t.begin(),t.end());
m[t].push_back(strs[i]);
}
unordered_map<string,vector<string> >::iterator it=m.begin();
while(it != m.end())
{
res.push_back(it->second);
it++;
}
return res;
}
};