10.2 Group Anagram

Simple string question.

    void groupAnagrams(vector<string>& strs, vector<string>& res) {
    unordered_map<string, multiset<string>> mp;
    for (string s : strs) {
        string t = s;
        sort(t.begin(), t.end());
        mp[t].insert(s);
    }
    for (auto m : mp) {
        for(string s : m.second){
            res.push_back(s);
        }
    }
}

你可能感兴趣的:(sorting)