Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
思路:用哈希表储存
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
unordered_map<string, vector<string> > map;
vector<string> res;
if (strs.size() == 0) {
return res;
}
for (auto iter = strs.begin(); iter != strs.end(); iter++) {
auto key = *iter;
sort(key.begin(), key.end());
map[key].push_back(*iter);
}
for (auto iter = map.cbegin(); iter != map.cend(); iter++) {
if (iter->second.size() > 1) {
res.insert(res.end(), iter->second.begin(), iter->second.end());
}
}
return res;
}
};