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;
    }
};

相关文章:

  • 2021-06-05
  • 2022-12-23
  • 2021-07-08
  • 2021-07-17
  • 2021-07-26
  • 2022-03-09
猜你喜欢
  • 2022-12-23
  • 2021-06-12
  • 2022-01-02
相关资源
相似解决方案