class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
string s;
map<string, int> anagram;
vector<string> res;
for (int i = 0; i < strs.size(); ++i) {
s = strs[i];
sort(s.begin(), s.end());
if (anagram.find(s) == anagram.end()) {
anagram[s] = i;
} else {
if (anagram[s] >= 0) {
res.push_back(strs[anagram[s]]);
anagram[s] = -1;
}
res.push_back(strs[i]);
}
}
return res;
}
};
Anagrams
最新推荐文章于 2021-02-23 20:43:02 发布