Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<string>> groupAnagrams(vector<string>& strs) {
  4.         unordered_map<string, vector<string>> table;
  5.         for (int i = 0; i < strs.size(); i++) {
  6.             int count[26] = {0};
  7.             for (char c: strs[i]) {
  8.                 count[c - 'a']++;
  9.             }
  10.             string key = "";
  11.             for (int j = 0; j < 26; j++) {
  12.                 key += to_string(count[j]);
  13.             }
  14.             auto it = table.find(key);
  15.             if (it == table.end()) {
  16.                 table.insert({key, vector<string>()});
  17.                 it = table.find(key);
  18.             }
  19.             it->second.push_back(strs[i]);
  20.         }
  21.         vector<vector<string>> res;
  22.         for (auto it = table.begin(); it != table.end(); it++){
  23.             res.push_back(it->second);
  24.         }
  25.         return res;
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement