Talentd
FleetCode
Make and follow sheets.Sign in

Check if The Number is Fascinating solution and explanation

EasyHash TableMath4 min read53.0% acceptanceUpdated 9 Apr 2026
Check if The Number is Fascinating || Biweekly Contest 106 solution with ExplanationLearn to Code · 3:28 · 316 viewsWatch on YouTube
9 more videos on this problem
  1. Check if The Number is Fascinating || Biweekly Contest 106 solution with ExplanationLearn to Code · 3:28 · 316 views
  2. Check if The Number is Fascinating | Leetcode 2729 | BiWeekly 106Tech Courses · 4:24 · 258 views
  3. 6461. Check if The Number is Fascinating | Leetcode Biweekly Contest 106 | Full solutionCodeFod · 3:10 · 244 views
  4. 2729. Check if The Number is Fascinating (Leetcode Easy)Programming Live with Larry · 4:06 · 200 views
  5. Leetcode BiWeekly contest 106 - Easy - Check if The Number is FascinatingPrakhar Agrawal · 4:48 · 184 views
  6. 2729. Check if The Number is Fascinating || Biweekly || Leetcode || Solution ExplainCodeLuv · 10:00 · 107 views
  7. Solving LeetCode 2729: Check if The Number is Fascinating | Rust Tutorial | BiWeekly Contest 106vanAmsen · 3:46 · 43 views
  8. Cracking LeetCode 2729: Check if The Number is Fascinating | Python Tutorial | BiWeekly Contest 106vanAmsen · 3:58 · 42 views
  9. 2729. Check if The Number is FascinatingmoodyCode · 10:25 · 35 views
  10. Day 731: LeetCode Problem 2729. Check if The Number is Fascinating - SwiftiOSxBank · 7:00 · 15 views

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.

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.

def is_fascinating(n: int) -> bool:
    combined = f"{n}{2*n}{3*n}"
    return set(combined) == set('123456789')

print(is_fascinating(192))
import java.util.HashSet;

public class FascinatingNumber {
    public static boolean isFascinating(int n) {
        String combined = Integer.toString(n) + Integer.toString(2 * n) + Integer.toString(3 * n);
        HashSet<Character> digits = new HashSet<>();
        for (char c : combined.toCharArray()) {
            if (c == '0' || !digits.add(c)) return false;
        }
        return digits.size() == 9;
    }

    public static void main(String[] args) {
        int n = 192;
        System.out.println(isFascinating(n));
    }
}
#include <iostream>
#include <unordered_set>
#include <string>

bool isFascinating(int n) {
    std::string result = std::to_string(n) + std::to_string(2 * n) + std::to_string(3 * n);
    std::unordered_set<char> digits(result.begin(), result.end());
    return digits.size() == 9 && digits.find('0') == digits.end();
}

int main() {
    int n = 192;
    std::cout << std::boolalpha << isFascinating(n) << std::endl;
    return 0;
}
function isFascinating(n) {
    const combined = n.toString() + (2 * n).toString() + (3 * n).toString();
    const digits = new Set(combined);
    return digits.size === 9 && !digits.has('0');
}

console.log(isFascinating(192));
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

bool isFascinating(int n) {
    char result[100];
    sprintf(result, "%d%d%d", n, 2*n, 3*n);
    int count[10] = {0};

    for (int i = 0; i < strlen(result); i++) {
        int digit = result[i] - '0';
        if (digit == 0 || count[digit] > 0) return false;
        count[digit]++;
    }

    for (int i = 1; i <= 9; i++) {
        if (count[i] != 1) return false;
    }

    return true;
}

int main() {
    int n = 192;
    printf("%s\n", isFascinating(n) ? "true" : "false");
    return 0;
}
using System;
using System.Collections.Generic;

class FascinatingNumber {
    public static bool IsFascinating(int n) {
        string combined = n.ToString() + (2 * n).ToString() + (3 * n).ToString();
        HashSet<char> digits = new HashSet<char>(combined);
        return digits.Count == 9 && !digits.Contains('0');
    }

    static void Main() {
        int n = 192;
        Console.WriteLine(IsFascinating(n));
    }
}

The solution converts n, 2*n, and 3*n to strings and concatenates them. It then checks if the set of resulting digits equals the set {'1', '2', '3', '4', '5', '6', '7', '8', '9'}. This ensures all and only the digits 1-9 are present.

Complexity

Time Complexity: O(1) as set operations handle a fixed-size collection.
Space Complexity: O(1) since only extra storage for a fixed-length set 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.

def is_fascinating(n: int) -> bool:
    combined = f"{n}{2*n}{3*n}"
    return ''.join(sorted(combined)) == '123456789'

print(is_fascinating(192))
import java.util.Arrays;

public class FascinatingNumber {
    public static boolean isFascinating(int n) {
        char[] result = (n + "" + (2 * n) + "" + (3 * n)).toCharArray();
        Arrays.sort(result);
        return new String(result).equals("123456789");
    }

    public static void main(String[] args) {
        int n = 192;
        System.out.println(isFascinating(n));
    }
}
#include <iostream>
#include <algorithm>

bool isFascinating(int n) {
    std::string result = std::to_string(n) + std::to_string(2 * n) + std::to_string(3 * n);
    std::sort(result.begin(), result.end());
    return result == "123456789";
}

int main() {
    int n = 192;
    std::cout << std::boolalpha << isFascinating(n) << std::endl;
    return 0;
}
function isFascinating(n) {
    const combined = `${n}${2 * n}${3 * n}`;
    return combined.split('').sort().join('') === '123456789';
}

console.log(isFascinating(192));
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int comp(const void *a, const void *b) {
    return *(char *)a - *(char *)b;
}

bool isFascinating(int n) {
    char combined[10];
    sprintf(combined, "%d%d%d", n, 2 * n, 3 * n);
    qsort(combined, 9, sizeof(char), comp);
    return strcmp(combined, "123456789") == 0;
}

int main() {
    int n = 192;
    printf("%s\n", isFascinating(n) ? "true" : "false");
    return 0;
}
using System;

class FascinatingNumber {
    public static bool IsFascinating(int n) {
        char[] combined = (n.ToString() + (2 * n).ToString() + (3 * n).ToString()).ToCharArray();
        Array.Sort(combined);
        return new string(combined) == "123456789";
    }

    static void Main() {
        int n = 192;
        Console.WriteLine(IsFascinating(n));
    }
}

In Python, concatenated strings of n, 2*n, and 3*n are sorted and then compared to "123456789". This direct comparison allows straightforward verification of fascinating conditions.

Complexity

Time Complexity: O(1), since operations on a constant number of digits.
Space Complexity: O(1), space used is minimal and constant.

Try this approach in the editor

Approach 3: Default Approach

function isFascinating(n: number): boolean {
    const s = `${n}${n * 2}${n * 3}`;
    return s.split('').sort().join('') === '123456789';
}
func isFascinating(n int) bool {
	s := strconv.Itoa(n) + strconv.Itoa(n*2) + strconv.Itoa(n*3)
	cnt := [10]int{}
	for _, c := range s {
		cnt[c-'0']++
		if cnt[c-'0'] > 1 {
			return false
		}
	}
	return cnt[0] == 0 && len(s) == 9
}
impl Solution {
    pub fn is_fascinating(n: i32) -> bool {
        let s = format!("{}{}{}", n, n * 2, n * 3);

        let mut cnt = vec![0; 10];
        for c in s.chars() {
            let t = (c as usize) - ('0' as usize);
            cnt[t] += 1;
            if cnt[t] > 1 {
                return false;
            }
        }

        cnt[0] == 0 && s.len() == 9
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Set-Based Unique Digit Verification

Time Complexity: O(1) as set operations handle a fixed-size collection.
Space Complexity: O(1) since only extra storage for a fixed-length set is needed.

Direct Character Comparison

Time Complexity: O(1), since operations on a constant number of digits.
Space Complexity: O(1), space used is minimal and constant.

Default Approach–

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.

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.

Comments

Now write it yourselfClose this page and solve Check if The Number is Fascinating in the editor, without looking.
Practise Check if The Number is Fascinating