天天看点

力扣-49题 字母异位词分组(C++)- 哈希、折中化思想

题目链接:https://leetcode-cn.com/problems/group-anagrams/

题目如下:

力扣-49题 字母异位词分组(C++)- 哈希、折中化思想
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        //思路:A和B同时难以让对方同化,方式——折中化出一个C
        vector<vector<string>> result;

        unordered_map<string,vector<string>> hash;
        for(auto& str:strs){
            string temp=str;
            sort(temp.begin(),temp.end());//对每个字符串排序,折中化
            hash[temp].push_back(str);
        }

        for(auto& e:hash){
            result.push_back(e.second);
        }

        return result;

    }
};