class Solution {
public:
vector<string> anagrams(vector<string>& strs)
{
map<string,int> m;
vector<string> res;
if(strs.empty())
return res;
const int n=strs.size();
for(int i=0;i<n;++i)
{
string tmp=strs[i];
sort(tmp.begin(),tmp.end());
if(m.find(tmp)==m.end())
m[tmp]=i;
else
{
if(m[tmp]==-1)
res.push_back(strs[i]);
else
{
res.push_back(strs[m[tmp]]);
res.push_back(strs[i]);
m[tmp]=-1;
}
}
}
return res;
}
};
Anagrams
最新推荐文章于 2021-02-23 20:43:02 发布