字母异位词分组

问题描述:
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:
输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出:
[
[“ate”,“eat”,“tea”],
[“nat”,“tan”],
[“bat”]
]

说明:
所有输入均为小写字母。
不考虑答案输出的顺序。

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/group-anagrams

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> hash;
        vector<vector<string>> res;
        for(string s:strs){
            string t=s;
            sort(t.begin(),t.end());
            hash[t].push_back(s);
        }
        for(auto n:hash){
            res.push_back(n.second);
        }
        return res;
    }
};

版权声明:本文为ZhEnG_jIe_YuAn原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。