Skip to main content

Check if The Number is Fascinating - Solution & Explanation

EasyHash TableMath12 min read
Practice this problem

Problem Statement

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.

Return true if n is fascinating, or false otherwise.

Concatenating two numbers means joining them together. For example, the concatenation of 121 and 371 is 121371.

 

Example 1:

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.

Example 2:

Input: n = 100
Output: false
Explanation: We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions.

 

Constraints:

  • 100 <= n <= 999

Approach Overview

Problem Overview: A number is called fascinating if the concatenation of n, 2 * n, and 3 * n forms a 9-digit string containing each digit from 1 to 9 exactly once. Any repetition, missing digit, or the presence of 0 means the number is not fascinating.

Approach 1: Set-Based Unique Digit Verification (Time: O(d), Space: O(d))

Compute 2 * n and 3 * n, then concatenate them with n to form a single string. Iterate through the characters and insert each digit into a set. If the string length is not 9, or the set contains 0, or the final set size is not 9, the number fails the fascinating condition. The set guarantees uniqueness while iteration verifies all digits are valid. This approach relies on constant‑size digit processing, making the complexity effectively O(1) since d ≤ 9.

This method uses concepts similar to a hash lookup, where insertion and membership checks happen in constant time. If you want to review the underlying structure, see hash table fundamentals.

Approach 2: Direct Character Comparison (Time: O(d), Space: O(1))

Instead of storing digits in a set, concatenate n, 2n, and 3n into a string and count digit frequencies using a fixed array of size 10. Iterate through the string and increment the counter for each digit. The number is fascinating only if digits 1 through 9 each appear exactly once and digit 0 never appears. This avoids dynamic structures and relies purely on index access.

This technique works well because the problem is driven by digit manipulation and simple arithmetic operations. If you want to explore more problems based on number properties, review math techniques commonly used in interview questions.

Recommended for interviews: The set-based approach is usually the first solution developers write because it clearly expresses the “unique digits” requirement. The frequency-array version is slightly more optimized and avoids extra structures. Showing the set approach first demonstrates clear reasoning, while the direct counting method shows attention to constant-space optimization.

Approach 1: Set-Based Unique Digit Verification

This approach uses a set to verify the uniqueness of digits. After concatenating n, 2*n, and 3*n, we convert the result into a set of characters. The number is considered fascinating if the set contains exactly the digits '1' to '9', and if its length is 9.

We use sprintf to concatenate n, 2*n, and 3*n into a string. We then count the frequency of each digit using an array. If the digit '0' appears or any digit appears more than once, we return false. Otherwise, we verify if all digits from 1 to 9 appear exactly once.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because the number of digits is constant.
Space Complexity: O(1) because only a fixed amount of extra space is needed.

Try this approach in the editor →

Approach 2: Direct Character Comparison

This approach constructs the concatenated string from n, 2*n, and 3*n and directly checks if the resulting string matches the sorted version of '123456789'. Since the order must not affect the presence of digits between 1 to 9, sorting provides a straightforward comparison for validation.

In this C solution, the concatenated result of n, 2*n, and 3*n is stored in a character array. We use qsort to sort the characters and then check if the resultant sorted string is "123456789". This determines the number's fascinating status.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1), due to constant and fixed string size.
Space Complexity: O(1), since we're using predefined character buffers.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Set-Based Unique Digit Verification

Time Complexity: O(1) because the number of digits is constant.
Space Complexity: O(1) because only a fixed amount of extra space is needed.

Direct Character Comparison

Time Complexity: O(1), due to constant and fixed string size.
Space Complexity: O(1), since we're using predefined character buffers.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Set-Based Unique Digit VerificationO(d)O(d)Best for readability and quick implementation when checking digit uniqueness.
Direct Character Comparison (Frequency Array)O(d)O(1)Useful when minimizing memory usage or avoiding hash-based structures.

Video Solution

Check if The Number is Fascinating || Biweekly Contest 106 solution with ExplanationLearn to Code316 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Check if The Number is Fascinating easy or hard?
LeetCode classifies this problem as Easy. The main challenge is understanding the definition of a fascinating number and verifying digit uniqueness. Once the concatenation step is clear, the implementation becomes a straightforward digit validation task.
Check if The Number is Fascinating Python/Java solution
The typical Python or Java solution concatenates n, 2*n, and 3*n into a string and validates digits using a set or a frequency array. The algorithm iterates once over the digits and checks that digits 1 through 9 appear exactly once. This keeps the implementation short and runs in constant time for the given constraints.
How to solve Check if The Number is Fascinating in O(n)?
Form the string representation of n, 2n, and 3n, then scan each digit once. Track occurrences using a set or a fixed-size array of digit counts. If the final string length is 9 and digits 1–9 appear exactly once with no zeros, the number is fascinating. This requires a single pass through the digits.
What is the best approach for Check if The Number is Fascinating?
The most practical approach concatenates n, 2n, and 3n into a single string and verifies that digits 1 through 9 appear exactly once. A set-based check is commonly used because insertion automatically removes duplicates and membership checks are O(1). Since the resulting string has at most 9 digits, the algorithm runs in constant time in practice.
Is Check if The Number is Fascinating asked at Google/Amazon/Meta?
Problems based on digit properties and validation appear frequently in coding interviews, especially for screening rounds. While this exact problem may not be asked directly, similar questions involving digit uniqueness, string concatenation, and number manipulation appear in interview banks used by companies like Amazon and Google.
What data structure is used in Check if The Number is Fascinating?
Most solutions use a hash set to track unique digits while iterating through the concatenated number string. An alternative implementation uses a fixed array of size 10 to count digit frequencies. Both approaches provide constant-time checks for digit presence.
What is the time complexity of Check if The Number is Fascinating?
The time complexity is O(d), where d is the number of digits in the concatenated string n + 2n + 3n. Because the maximum length is 9 digits, the complexity behaves as O(1) in practice. Space complexity is O(d) for the set approach or O(1) when using a fixed-size frequency array.

Ready to solve this problem?

Practice Check if The Number is Fascinating with our built-in code editor and test cases.

Practice on FleetCode