Share My short C++ solution. It takes 60ms!


  • 3
    F

    No highlight in idea. The only trick is to use the 'swap' function to avoid another copy overhead.
    Easy to understand. Can‘t be more concise.

    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string, vector<string>> hashMap;
            for(auto &v : strs) {
                string tmp(v);
                sort(tmp.begin(), tmp.end());
                hashMap[tmp].push_back(v);
            }
            vector<vector<string>> result(hashMap.size());
            int k = 0;
            for(auto it = hashMap.begin(); it != hashMap.end(); ++it, ++k) {
                result[k].swap(it->second);
                sort(result[k].begin(), result[k].end());
            }
            return result;
        }
    };

Log in to reply
 

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