class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
string s;
map<string,int> m;
vector<string> result;
for(int i = 0;i < strs.size();i ++){
string s = strs[i];
sort(s.begin(),s.end());
if(m.find(s) == m.end())
m[s] = i;
else{
if(m[s] == -1)
result.push_back(strs[i]);
else{
result.push_back(strs[m[s]]);
m[s] = -1;
result.push_back(strs[i]);
}
}
}
return result;
}
};
[LeetCode] Anagrams
最新推荐文章于 2020-07-07 20:25:37 发布