-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.cpp
71 lines (64 loc) · 1.63 KB
/
Solution.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class Solution {
private:
unsigned long prime = 51;
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<unsigned long> hash;
vector<vector<string>> res;
for (string str : strs) {
unsigned s_hash = getHash(str);
bool found = false;
for (int i = 0; i < hash.size(); i ++) {
if (hash[i] == s_hash) {
res[i].push_back(str);
found = true;
break;
}
}
if (not found) {
hash.push_back(s_hash);
res.push_back({str});
}
}
return res;
}
unsigned long getHash(string str) {
int cnt[26] = {0};
for (char c : str) {
cnt[c - 'a'] ++ ;
}
unsigned long res = 0;
for (int i = 0; i < 26; i++) {
res += cnt[i] * pow(prime, i);
}
return res;
}
// O(1)
unsigned long pow(unsigned long x, unsigned int y) {
// bitwise traverse y
unsigned long res = 1;
for (int offset = sizeof(int) * 8 - 1; offset >= 0; offset --) {
res *= res; // right shift
if (y & (1 << offset)) res *= x;
}
return res;
}
};
int main() {
Solution a;
vector<string> b = {"eat","tea","tan","ate","nat","bat"};
a.groupAnagrams(b);
return 0;
}