All problems
0929MediumMathCounting

Digits That Rebuild Their Factorial Total

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3848Check Digitorial Permutation

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Take each digit of n, work out its factorial, and add those factorials together. Call the result the factorial total of n, taking 0! to be 1.

Return true when the digits of the factorial total are a rearrangement of the digits of n, that is when the two numbers use exactly the same digits the same number of times.

Examples

Example 1

Input
n = 154
Output
true

The digit factorials are 1, 120 and 24, which come to 145. Those digits are a rearrangement of 1, 5 and 4.

Example 2

Input
n = 123
Output
false

The digit factorials 1, 2 and 6 come to 9, which uses a digit that 123 does not.

Example 3

Input
n = 40585
Output
true

The digit factorials 24, 1, 120, 40320 and 120 come to 40585, the number itself.

Constraints

  • 1 <= n <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def is_digitorial_permutation(n: int) -> bool:
Java
public boolean isDigitorialPermutation(int n)
September 7
Apply