Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
使用map来存排序后的string和该string的位置,每次找到一个相同的之后就把位置置-1,就可以分辨那些已经把值放进result哪些没有
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> result;
map<string,int> myMap;
map<string,int>::iterator it;
for(int i=0;i<strs.size();i++){
string temp=strs[i];
sort(temp.begin(),temp.end());
it=myMap.find(temp);
if(it==myMap.end())
myMap[temp]=i;
else{
result.push_back(strs[i]);
if(myMap[temp]!=-1){
result.push_back(strs[myMap[temp]]);
myMap[temp]=-1;
}
}
}
return result;
}
};