天天看點

力扣-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;

    }
};