Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
判断是不是回文就是把string 排序,然后哈希看是否有一样的存在。这里用了map来存取。
1. 排序
2. 如果没有那么把index存到map去,如果有,那么存到结果并且检查map里以前的有没有被存进去,如果还有没存的,存并且flag
3. 循环到底
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
map<string,int> mp;
vector<string> res;
for (int i=0; i<strs.size(); i++){
string s=strs[i];
std::sort(s.begin(),s.end());
if (mp.find(s)!=mp.end()){
res.push_back(strs[i]);
if(mp[s]!=-1){
res.push_back(strs[mp[s]]);
mp[s]=-1;
}
} else
mp[s]=i;
}
return res;
}
};