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> res;
map<string,int> mymap;
for(int i=0;i<strs.size();++i){
string temp=strs[i];
sort(temp.begin(),temp.end());
if(mymap.find(temp)!=mymap.end()){
if(mymap[temp]!= -1)
res.push_back(strs[mymap[temp]]);
mymap[temp]=-1;
res.push_back(strs[i]);
}
else
mymap[temp]=i;
}
return res;
}
};