0049.cpp (685B)
1 class Solution { 2 public: 3 vector<vector<string>> groupAnagrams(vector<string> &strs) { 4 vector<vector<string>> res; 5 unordered_map<unsigned long long, int> um; 6 for (int i = 0; i < strs.size(); i++) { 7 unsigned long long hash = 0; 8 vector<int> m(26, 0); 9 10 for (char c : strs[i]) 11 m[c - 'a']++; 12 for (int i = 0; i < 26; i++) 13 hash = hash * 100 + m[i]; 14 if (um.find(hash) == um.end()) { 15 um[hash] = res.size(); 16 res.push_back({strs[i]}); 17 } else 18 res[um[hash]].push_back(strs[i]); 19 } 20 return res; 21 } 22 };