| 17 | |
| 18 | public: |
| 19 | vector<vector<string>> groupAnagrams(vector<string>& strs) { |
| 20 | vector<unsigned long> hash; |
| 21 | vector<vector<string>> res; |
| 22 | for (string str : strs) { |
| 23 | unsigned s_hash = getHash(str); |
| 24 | bool found = false; |
| 25 | for (int i = 0; i < hash.size(); i ++) { |
| 26 | if (hash[i] == s_hash) { |
| 27 | res[i].push_back(str); |
| 28 | found = true; |
| 29 | break; |
| 30 | } |
| 31 | } |
| 32 | if (not found) { |
| 33 | hash.push_back(s_hash); |
| 34 | res.push_back({str}); |
| 35 | } |
| 36 | } |
| 37 | return res; |
| 38 | } |
| 39 | |
| 40 | unsigned long getHash(string str) { |
| 41 | int cnt[26] = {0}; |