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

  • 3

    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 {
        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());
            vector<vector<string>> result(hashMap.size());
            int k = 0;
            for(auto it = hashMap.begin(); it != hashMap.end(); ++it, ++k) {
                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.