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
| const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<long, vector<string> > hash_map; for (auto &&str : strs) hash_map[key(str)].push_back(str); vector<vector<string> > ret(hash_map.size()); size_t i(0); for (auto &&pair : hash_map) { ret[i].swap(pair.second); sort(ret[i].begin(), ret[i].end()); ++i; } return ret; }
private: long key(const string &str) { long ret(1); for (auto &&c : str) ret *= primes[c - 'a']; return ret; } };
|