[LeetCode] Check Digitorial Permutation

3848. Check Digitorial Permutation

You are given an integer n.

A number is called digitorial if the sum of the factorials of its digits is equal to the number itself.

Determine whether any permutation of n (including the original order) forms a digitorial number.

Return true if such a permutation exists, otherwise return false.

Note:

  • The factorial of a non-negative integer x, denoted as x!, is the product of all positive integers less than or equal to x, and 0! = 1.
  • A permutation is a rearrangement of all the digits of a number that does not start with zero. Any arrangement starting with zero is invalid.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
bool isDigitorialPermutation(int n) {
int fact[10]{0,};
for(int i = 0; i < 10; i++) {
fact[i] = 1;
for(int j = 1; j <= i; j++) fact[i] *= j;
}
int sum = 0, x = n;
while(x) {
sum += fact[x%10];
x /= 10;
}
unordered_map<int,int> freq;
while(n) {
freq[n%10]++; n/=10;
}
while(sum) {
freq[sum%10]--; sum/= 10;
}
for(auto& [k,v] : freq) if(v) return false;
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/check-digitorial-permutation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.