-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path49.cpp
More file actions
executable file
·26 lines (26 loc) · 802 Bytes
/
49.cpp
File metadata and controls
executable file
·26 lines (26 loc) · 802 Bytes
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
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& strs) {
vector<vector<string> > answer;
unordered_map<string, vector<string>> m;
for (int i = 0; i < strs.size(); i++){
string cur = strs[i];
vector<int> cnt(26);
for (int j = 0; j < cur.size(); j++)
cnt[int(cur[j]-'a')]++;
string t = "";
for (int j = 0; j < 26; j++)
if (cnt[j] == 0) continue;
else t += string(1, j + 'a') + to_string(cnt[j]);
m[t].push_back(cur);
}
for (auto a : m)
answer.push_back(a.second);
return answer;
}
};