Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<string>> groupAnagrams(vector<string>& strs) {
- unordered_map<string, vector<string>> table;
- for (int i = 0; i < strs.size(); i++) {
- int count[26] = {0};
- for (char c: strs[i]) {
- count[c - 'a']++;
- }
- string key = "";
- for (int j = 0; j < 26; j++) {
- key += to_string(count[j]);
- }
- auto it = table.find(key);
- if (it == table.end()) {
- table.insert({key, vector<string>()});
- it = table.find(key);
- }
- it->second.push_back(strs[i]);
- }
- vector<vector<string>> res;
- for (auto it = table.begin(); it != table.end(); it++){
- res.push_back(it->second);
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement