A vault manufacturer engraves every lock with an n-digit combination code (the code never begins with a leading zero unless it is the single-digit code 0). Security auditors compute the code's ignition value: the sum, over every digit d appearing in the code, of d! (with 0! = 1). A code is declared self-consistent if some rearrangement of its own digits, written without any extra leading zeros, reproduces the ignition value exactly -- equivalently, the decimal representation of the ignition value must use exactly the same digits, the same number of times, as the code itself. Determine whether the given code is self-consistent.
A single line containing the code, a string of decimal digits, 1 <= length <= 18, with no leading zero unless the code is exactly 0.
Print true if the code is self-consistent, otherwise print false.
Example 1
Input
145
Expected
true
Explanation
145 is a classic self-referential code: 1! + 4! + 5! = 1 + 24 + 120 = 145, and the digits of 145 are exactly the digits 1, 4, 5 already present in the code, so it is self-consistent and the output is true.
Example 2
Input
230
Expected
false
Explanation
The ignition value is 2! + 3! + 0! = 2 + 6 + 1 = 9, a single digit. Its digit multiset {9} does not match the code's digit multiset {2, 3, 0}, so the code is not self-consistent and the output is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →