68ms,Pure C++ 11 Style Solution using unordered map and multisite,9 lines Very Easy to Understand!


  • 2
    H
    class Solution {
    public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> ret;
        unordered_map<string,multiset<string>> mp;
        for(auto &x:strs)
        {
            auto tmp = x;
            sort(tmp.begin(),tmp.end());
            mp[tmp].insert(x);
        }
        for(auto &x:mp)
            ret.push_back(vector<string>(x.second.begin(),x.second.end()));
        return ret;
       }
    };

Log in to reply
 

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