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> result;
int size = strs.size();
if (size < 1)
{
return result;
}
map<string, string> table;
for (int i = 0; i < size; i++)
{
string temp = strs[i];
sort(temp.begin(), temp.end());
map<string, string>::iterator it = table.find(temp);
if (it != table.end())
{
if (temp.length() != it->second.length())
{
it->second = it->second.substr(1);
result.push_back(it->second);
}
result.push_back(strs[i]);
}
else
{
table[temp] = "N" + strs[i];
}
}
return result;
}
};