LeetCode Problem Workspace

Check if The Number is Fascinating

Determine if a 3-digit number is fascinating by checking if the concatenated result of n, 2*n, and 3*n contains all digits from 1 to 9.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Hash Table plus Math

bolt

Answer-first summary

Determine if a 3-digit number is fascinating by checking if the concatenated result of n, 2*n, and 3*n contains all digits from 1 to 9.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Hash Table plus Math

Try AiBox Copilotarrow_forward

A fascinating number is a 3-digit number such that the concatenated result of n, 2 n, and 3 n contains exactly the digits from 1 to 9 once and no zeros. The task is to determine whether the given number n satisfies this condition.

Problem Statement

You are given a 3-digit integer n. A number is called fascinating if, after concatenating n, 2 n, and 3 n together, the resulting string contains all digits from 1 to 9 exactly once, without any zero.

Return true if n is fascinating, or false otherwise. For example, for n = 192, the concatenated result is '192384576', which contains all digits from 1 to 9. For n = 100, the concatenated result '100200300' is not valid as it contains zeroes and does not meet the criteria.

Examples

Example 1

Input: n = 192

Output: true

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

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

Solution Approach

Concatenate n, 2 n, and 3 n

Concatenate the number n, 2 n, and 3 n. This will give a 9-digit number if n is a valid input. The next step is to check if the concatenated result contains exactly the digits from 1 to 9.

Validate Digits

After concatenating, check if the resulting number contains exactly one occurrence of each digit from 1 to 9, and ensure there are no zeroes. A hash table can be useful to verify each digit’s frequency.

Return the Result

If the conditions are met, return true, indicating that the number is fascinating. Otherwise, return false, meaning the number does not meet the criteria.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the approach used to check the digits and concatenation. Typically, it is O(1) because the input is limited to 3 digits. The space complexity also depends on the method of digit storage and checking, which can be optimized with hash tables.

What Interviewers Usually Probe

  • Checks the candidate’s understanding of string manipulation and number properties.
  • Looks for familiarity with hash tables for efficient validation of digits.
  • Evaluates problem-solving approach in terms of validation and optimization.

Common Pitfalls or Variants

Common pitfalls

  • Not handling the case where the number contains zeroes.
  • Misunderstanding the requirement for exactly one occurrence of each digit from 1 to 9.
  • Not considering how to efficiently check the digits of the concatenated number.

Follow-up variants

  • Test with different ranges of n (e.g., edge cases with n = 100 or n = 999).
  • Try modifying the problem to work with numbers of different lengths.
  • Consider variations where the number could be non-three-digit and evaluate the change in constraints.

FAQ

What is a fascinating number in this problem?

A fascinating number is a 3-digit number n such that when n, 2 n, and 3 n are concatenated, the resulting number contains each of the digits from 1 to 9 exactly once and no zeroes.

How do I check if a number is fascinating?

You concatenate n, 2 n, and 3 n, then check if the resulting string contains each digit from 1 to 9 exactly once and no zeros.

What should I do if the number contains zeros?

If the concatenated result contains zeros, the number is not fascinating and you should return false.

Is this problem related to hashing?

Yes, you can use a hash table to efficiently check the frequency of digits in the concatenated number.

Can this problem be solved using a brute force approach?

Yes, a brute force approach can work, but using a hash table for digit validation can improve efficiency.

terminal

Solution

Solution 1

#### Python3

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

Solution 2

#### Rust

1
2
3
4
class Solution:
    def isFascinating(self, n: int) -> bool:
        s = str(n) + str(2 * n) + str(3 * n)
        return "".join(sorted(s)) == "123456789"
Check if The Number is Fascinating Solution: Hash Table plus Math | LeetCode #2729 Easy