36 ms c++ solution


  • 0
    J
    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string, vector<string>> hash;
            vector<vector<string>> res;
        
            for (int i = 0; i < strs.size(); i++) {
                string key = strs[i];
                sort(key.begin(), key.end());
                hash[key].push_back(strs[i]);
            }
        
            for (auto it = hash.begin(); it != hash.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.