Show problems as
Classic problem #3848MediumMathCounting

Check Digitorial Permutation

Real-world version: Digits That Rebuild Their Factorial Total
Tracked in this browser only
Read Check Digitorial Permutation on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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