leetcode

49.字母异位词分组

力扣链接

class Solution {
public:
    std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string>& strs) {
        std::vector<std::vector<std::string>> res;
        // 同一个键关联多个值 键是string 值是string向量集合
        std::unordered_map<std::string, std::vector<std::string>> map;
        for (std::string s:strs){
            std::string key = s;
            std::sort(key.begin(), key.end());
            // 同一个键关联多个值
            map[key].emplace_back(s);
        }
        for (auto it=map.begin(); it!=map.end();it++)
            res.emplace_back(it->second);
        return res;
    }
};