Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
Solution:Code:
<span style="font-size:14px;">class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
const int length = strs.size();
vector<string> results;
if (length == 0) return results;
unordered_map<string, int> hashTable;
for (int i = 0; i < length; ++i) {
string temp = strs[i];
sort(temp.begin(), temp.end());
if (hashTable.find(temp) == hashTable.end())
hashTable[temp] = i;
else
if (hashTable[temp] == -1)
results.push_back(strs[i]);
else {
results.push_back(strs[hashTable[temp]]);
hashTable[temp] = -1;
results.push_back(strs[i]);
}
}
return results;
}
};</span>