using unordered_map, beat 86.43%


  • 0
    Y

    ···
    class Solution {
    public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
    vector<vector<string>> res;
    int sz = strs.size();
    if(!sz)
    return res;
    unordered_map<string, vector<int>> m;
    set<int> idx;
    vector<string> s(strs);
    for(int i = 0; i < sz; ++i){
    sort(strs[i].begin(), strs[i].end());
    m[strs[i]].push_back(i);
    }
    vector<string> tmpRes;
    for(auto &it: m){
    for(auto &i : (it.second)){
    tmpRes.push_back(s[i]);
    }
    res.push_back(tmpRes);
    tmpRes.clear();
    }
    return res;
    }
    };
    ····


Log in to reply
 

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