C++ unordered_map solution.


  • 0
    C
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> myMap;
        for (auto& str: strs) {
            string strCopy = str;
            sort(strCopy.begin(), strCopy.end());
            myMap[strCopy].push_back(str);
        }
        vector<vector<string>> res;
        for (auto it = myMap.begin(); it != myMap.end(); it++) {
            sort(it->second.begin(), (*it).second.end());
            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.