問題
https://leetcode.com/problems/anagrams/
代碼
使用hash + set
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, multiset<string>> mp;
for(int i=0; i< strs.size(); ++i)
{
string t = strs[i];
sort(t.begin(), t.end());
mp[t].insert(strs[i]);
}
vector<vector<string>> ret;
unordered_map<string, multiset<string>>::iterator it;
for (it = mp.begin(); it!= mp.end(); ++it)
{
ret.push_back(vector<string>(it->second.begin(), it->second.end()));
}
return ret;
}
};