Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
class Solution {
public:
std::vector<std::string> anagrams(std::vector<std::string> &strs) {
std::vector<std::string> res;
std::map<std::string, int> ans;
for (int i = 0; i < strs.size(); i++)
{
std::string s = strs[i];
std::sort(s.begin(),s.end());
if(ans.find(s) != ans.end())
{
if(ans[s] >= 0)
{
res.push_back(strs[ans[s]]);
ans[s] = -1;
}
res.push_back(strs[i]);
}
else
{
ans.insert(std::make_pair(s,i));
}
}
return res;
}
};