Given an array of strings, group anagrams together.
For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Return:
[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]
Note: All inputs will be in lower-case.
Subscribe to see which companies asked this question
代码:
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> ans;
if(strs.empty()) return ans;
map<string,vector<string>> m1;
for(auto p:strs)
{
string temp=p;
sort(temp.begin(),temp.end());
m1[temp].push_back(p);
}
for(auto p:m1)
{
ans.push_back(p.second) ;
}
return ans;
}
};