My 29ms simple C++, using unordered_map.


  • 0
    H

    """
    class Solution {
    public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
    unordered_map<string, vector<string>> hm;
    string key;
    vector<vector <string>> res;
    for (int i = 0; i < strs.size(); i++) {
    key = strs[i];
    std::sort(key.begin(), key.end());
    hm[key].emplace_back(strs[i]);
    }

        for (auto it = hm.begin(); it != hm.end(); it++) {
            res.push_back((*it).second);
        }
        return res;
    }
    

    };
    """


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.