题目
Number: 49
Difficulty: Medium
Tags: Hash Table, String
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
题解
找出所有回文组。
利用哈希表。
代码
vector<string> anagrams(vector<string>& strs) {
vector<string> res, ana;
unordered_map<string, vector<string>> mp;
for(string s : strs)
{
string temp = s;
sort(temp.begin(), temp.end());
mp[temp].push_back(s);
}
for(auto m : mp)
{
if(m.second.size() > 1)
res.insert(res.end(), m.second.begin(), m.second.end());
}
return res;
}