Check if The Number is Fascinating — LeetCode 2729 Python Solution

EasyHash TableMath
Problem
#2729
Reading time
2 min

The problem

You are given an integer n that consists of exactly 3 digits. We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0's: Concatenate n with the numbers 2 * n and 3 * n.

Example

Input
n = 192
Output
true
Explanation
We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.

Python solution

Python
class Solution:
    def isFascinating(self, n: int) -> bool:
        s = str(n) + str(2 * n) + str(3 * n)
        return "".join(sorted(s)) == "123456789"

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2729. Check if The Number is Fascinating is filed here because LeetCode tags it Math, which is the vocabulary this hub collects.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2729. Check if The Number is Fascinating?
LeetCode 2729. Check if The Number is Fascinating is rated Easy on LeetCode.
What is the time complexity of LeetCode 2729. Check if The Number is Fascinating?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2729. Check if The Number is Fascinating?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2729. Check if The Number is Fascinating cover?
LeetCode 2729. Check if The Number is Fascinating is tagged Hash Table and Math on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview