-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy paths1.cpp
More file actions
38 lines (38 loc) · 1020 Bytes
/
s1.cpp
File metadata and controls
38 lines (38 loc) · 1020 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
27
28
29
30
31
32
33
34
35
36
37
38
// OJ: https://leetcode.com/problems/letter-tile-possibilities/
// Author: github.com/lzl124631x
// Time: O(2^N)
// Space: O(N)
class Solution {
private:
int ans = 0, cnts[26] = {};
long long count(int n) {
long long val = 1;
for (int i = 1; i <= n; ++i) val *= i;
return val;
}
void updateAns() {
long long total = 0, div = 1;
for (int i = 0; i < 26; ++i) {
total += cnts[i];
div *= count(cnts[i]);
}
ans += count(total) / div;
}
void compute(string &A, int begin) {
if (begin == A.size()) {
updateAns();
return;
}
cnts[A[begin] - 'A']++;
compute(A, begin + 1);
cnts[A[begin] - 'A']--;
do { ++begin; } while (begin < A.size() && A[begin] == A[begin - 1]);
compute(A, begin);
}
public:
int numTilePossibilities(string tiles) {
sort(tiles.begin(), tiles.end());
compute(tiles, 0);
return ans - 1;
}
};