C++ 35ms Easy Solution using unordered map


  • 0
    S
    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            
            vector<vector<string>> result;
            unordered_map<string, vector<string>> my_map;
            
            for (int i=0; i<strs.size(); i++) {
                string sorted_word = strs[i];
                sort(sorted_word.begin(), sorted_word.end());
                my_map[sorted_word].push_back(strs[i]);
            }
            
            for (auto x: my_map) {
               result.push_back(x.second);
            }
            
            return result;
        }
    };
    

Log in to reply
 

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