Scribbling

[C++] 49. Group Anagrams 본문

Computer Science/C++

[C++] 49. Group Anagrams

focalpoint 2023. 8. 18. 05:26
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> map;
        for (string str : strs) {
            string sorted_str = str;
            sort(sorted_str.begin(), sorted_str.end());
            map[sorted_str].push_back(str);
        }
        vector<vector<string>> ret;
        for (auto e : map) {
            ret.push_back(e.second);
        }
        return ret;
    }
};