-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1079.cpp
More file actions
42 lines (39 loc) · 1.08 KB
/
1079.cpp
File metadata and controls
42 lines (39 loc) · 1.08 KB
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
class Solution {
public:
int fact(int x){
if(x <= 1) return 1;
return x * (x-1) * fact(x-2);
}
int numTilePossibilities(string tiles) { //O(N*2^N)
unordered_map<char, int> m;
int code = 1;
for(int i = 0; i < tiles.length(); i++){
if(m.count(tiles[i]) < 1){
m[tiles[i]] = code;
code *= 8;
}
}
unordered_set<int> s;
int total = 0;
for(int i = 1; i < (1 << tiles.length()); i++){
int subset = 0;
int n = 0;
for(int p = 0; p < tiles.length(); p++){
if(i & (1 << p)){
subset += m[tiles[p]];
n++;
}
}
if(s.count(subset) < 1){
s.insert(subset);
int nfact = fact(n);
while(subset > 0){
nfact /= fact(subset % 8);
subset /= 8;
}
total += nfact;
}
}
return total;
}
};