leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0049.cpp (685B)
0 class Solution {
1 public:
2 vector<vector<string>> groupAnagrams(vector<string> &strs) {
3 vector<vector<string>> res;
4 unordered_map<unsigned long long, int> um;
5 for (int i = 0; i < strs.size(); i++) {
6 unsigned long long hash = 0;
7 vector<int> m(26, 0);
9 for (char c : strs[i])
10 m[c - 'a']++;
11 for (int i = 0; i < 26; i++)
12 hash = hash * 100 + m[i];
13 if (um.find(hash) == um.end()) {
14 um[hash] = res.size();
15 res.push_back({strs[i]});
16 } else
17 res[um[hash]].push_back(strs[i]);
18 }
19 return res;
20 }
21 };