solution.cpp 537 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	vector<vector<string>> groupAnagrams(vector<string> &strs)
	{
		vector<vector<string>> res;
		unordered_map<string, vector<string>> ht;
		for (const auto &str : strs)
		{
			int counts[26] = {0};
			for (char c : str)
			{
				counts[c - 'a']++;
			}
			string key;
			for (int i : counts)
			{
				key.push_back('#');
				key.push_back(i + '0');
			}
			ht[key].push_back(str);
		}
		for (const auto &t : ht)
		{
			res.push_back(t.second);
		}
		return res;
	}
};