49.Anagrams

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;
    }
};
posted @ 2019-04-09 10:11  JohnRed  阅读(108)  评论(0编辑  收藏  举报