C++ short and easy understanding solution using map


  • 0
    Z
    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string,vector<string>> mp;
            for(auto &s:strs){
                string t=s;
                sort(t.begin(),t.end());
                mp[t].push_back(s);
            }
            vector<vector<string>> res;
            for(auto& i:mp){
                res.push_back(i.second);
            }
            return res;
        }
    };

Log in to reply
 

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