Leetcode: 49. Alphabetical anagram grouping

Use a hash table to solve the problem of anagram grouping.
The key of the map is a sorted string, and the value is the answer

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;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43579015/article/details/123472956