#3848
Medium Algorithms Check digitorial permutation
Math Counting
46.5% acceptance
Mar 15, 2026
58
2
You are given an integer n.
A number is digitorial if the sum of factorials of its digits equals the number itself.
Determine whether any permutation of n (not starting with zero) forms a digitorial number.
Return true if such a permutation exists, otherwise false.
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn is_digitorial_permutation(n: i32) -> bool {
let mut digits = Vec::new();
let mut tmp = n;
while tmp > 0 {
digits.push((tmp % 10) as u64);
tmp /= 10;
}
digits.sort();
let factorials: [u64; 10] = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880];
let digit_sum: u64 = digits.iter().map(|&d| factorials[d as usize]).sum();
let mut sum_digits = Vec::new();
let mut s = digit_sum;
if s == 0 { return false; }
while s > 0 {
sum_digits.push(s % 10);
s /= 10;
}
sum_digits.sort();
digits == sum_digits
}
}