力扣49. 字母异位词分组-Code

class Solution {
public:
    /**
     * Hash
     * 
     * @param strs Given string 
     * @return vector<vector<string>>
     */
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> map;
        for (string& str : strs) {
            string key = str;
            sort(key.begin(), key.end());
            map[key].push_back(str);
        }
        vector<vector<string>> anagramGroups;
        for (auto& pair : map) {
            anagramGroups.push_back(pair.second);
        }
        return anagramGroups;
    }
};
上一篇:栈和队列(Java实现)


下一篇:HTML