forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
53 lines (39 loc) · 1012 Bytes
/
main2.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
/// Source : https://leetcode.com/problems/letter-tile-possibilities/
/// Author : liuyubobobo
/// Time : 2019-06-15
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
/// Counting on the fly, based on frequency of characters
/// Time Complexity: O(n!)
/// Space Complexity: O(n)
class Solution {
public:
int numTilePossibilities(string tiles) {
unordered_map<char, int> freq;
for(char c: tiles)
freq[c] ++;
return dfs(freq);
}
private:
int dfs(unordered_map<char, int>& freq){
int res = 0;
for(char e = 'A'; e <= 'Z'; e ++)
if(freq[e]){
res ++;
freq[e] --;
res += dfs(freq);
freq[e] ++;
}
return res;
}
};
int main() {
cout << Solution().numTilePossibilities("AAB") << endl;
// 8
cout << Solution().numTilePossibilities("AAABBC") << endl;
// 188
return 0;
}