C++ solution using map


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

Log in to reply
 

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