class Solution {
public:
vector<string> anagrams(vector<string>& strs) {
vector<string> res;
map<string,int> mp;
for(int i=0;i<strs.size();i++)
{
string temp=strs[i];
if(temp.size())
sort(temp.begin(),temp.end());
if(mp.find(temp)==mp.end())
mp[temp]=i;//save the index of every sorted string
else
{
res.push_back(strs[i]);
if(mp[temp]!=-1)//first time an anagram shows up
{
res.push_back(strs[mp[temp]]);
mp[temp]=-1;//change the index to -1 to mark that it is saved
}
}
}
return res;
}
};