49. Group Anagrams

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

示例:

输入: ["eat", "tea", "tan", "ate", "nat", "bat"],
输出:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

说明:

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

C++:

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         vector<vector<string>> res;
 5         unordered_map<string,vector<string>> m ;
 6         for(string s : strs){
 7             string t = s ;
 8             sort(t.begin() , t.end()) ;
 9             m[t].push_back(s) ;
10         }
11         
12         for(auto p : m){
13             res.push_back(p.second) ;
14         }
15         return res ;
16     }
17 };
原文地址:https://www.cnblogs.com/mengchunchen/p/10861157.html