题目:
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) {
vector<string> res;
unordered_map<string, int> table;
for (int i = 0; i < strs.size(); i++) {
string s = strs[i];
sort(s.begin(), s.end());
//没出现过,加入
if (table.find(s) == table.end())
table[s] = i;
else {
if (table[s] != -1) {
res.push_back(strs[table[s]]);
table[s] = -1;
}
res.push_back(strs[i]);
}
}
return res;
}
};