C++ solution, beats 99%


  • 0
    M
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string, vector<string>> m;
            vector<vector<string>> r;
            
            for(const auto& str: strs)
                m[normalize(str)].push_back(str);
    
            for(const auto& p : m)
                r.push_back(p.second);
                
            return r;
        }
        
        string normalize(string s)
        {
            sort(s.begin(), s.end());
            return s;
        }
    

  • 0
    S

    so brief and so concise, thank you


Log in to reply
 

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