Leetcode:49.字母异位词分组

用哈希表解决字母异位词分组问题
map的key为排序好的字符串,value为答案

class Solution {
    
    
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
    
    
        unordered_map<string,vector<string>> mp;

        for(string& str : strs){
    
    

            string key = str;
            sort(key.begin(),key.end());
            mp[key].push_back(str);
        }

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

        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43579015/article/details/123472956