class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> res;
unordered_map<string,int> mmp;
if(strs.size()==0||strs.size()==1)return res;
for(int i=0;i<strs.size();++i){
string t=strs[i];
sort(t.begin(),t.end());
if(!mmp.count(t)){
mmp[t]=1;
}
else{
++mmp[t];
}
}
for(int i=0;i<strs.size();++i){
string t=strs[i];
sort(t.begin(),t.end());
if(mmp.count(t)&&mmp[t]>1){
res.push_back(strs[i]);
}
}
return res;
}
};
No comments:
Post a Comment