clueless coding // TODO: be smarter

LeetCode Group Anagrams: CPP Solution



TL;DR Code Solution


vector<vector<string>> groupAnagrams(vector<string>& strs) {

    unordered_map<string, vector<string>> map;

    for (int i = 0; i < strs.size(); i++) {
        string orig = strs[i];
        sort(strs[i].begin(), strs[i].end());
        map[strs[i]].push_back(orig);

    }

    vector<vector<string>> res;
    for (auto it = map.begin(); it != map.end(); it++) {
        res.push_back(it->second);    
    }
    return res;

}