49. 字母异位词分组

题目

代码

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

思路

利用map存储,key为排序后的字符串,value为排序前的字符串,因此所有排序后相同的字符串都属于一个分组。

https://github.com/li-zheng-hao
原文地址:https://www.cnblogs.com/lizhenghao126/p/11053607.html