-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3848_Check-Digitorial-Permutation.cpp
More file actions
60 lines (53 loc) · 1.5 KB
/
3848_Check-Digitorial-Permutation.cpp
File metadata and controls
60 lines (53 loc) · 1.5 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public:
int factorial(int val, int (&memo)[10]){
if(memo[val]) return memo[val];
return memo[val] = val * factorial(val-1, memo);
}
bool isDigitorialPermutation(int n) {
int memoFactorial[10] = {};
memoFactorial[0] = 1;
int anagramOrg[10] = {};
int anagramMod[10] = {};
int org = n, fact = 0;
while(n > 0){
int dig = n % 10;
++anagramOrg[dig];
n /= 10;
fact += factorial(dig, memoFactorial);
}
while(fact > 0){
++anagramMod[fact%10];
fact /= 10;
}
for(int i = 0; i < 10; ++i){
if(anagramOrg[i] != anagramMod[i]) return false;
}
return true;
}
};
class Solution {
public:
int factorial(int val, unordered_map<int,long long>& memo){
if(memo.count(val)) return memo[val];
return memo[val] = val * factorial(val-1, memo);
}
bool isDigitorialPermutation(int n) {
unordered_map<int,long long> memoFactorial{{0,1}};
unordered_map<int,int> anagramOrg;
unordered_map<int,int> anagramMod;
int org = n, fact = 0;
while(n > 0){
int dig = n % 10;
++anagramOrg[dig];
n /= 10;
fact += factorial(dig, memoFactorial);
}
while(fact > 0){
int dig = fact % 10;
++anagramMod[dig];
fact /= 10;
}
return anagramOrg == anagramMod;
}
};