Check if The Number is Fascinating — Problem & Solution
Check if The Number is Fascinating solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Check if The Number is Fascinating is an easy coding problem on strings, hash table and math, asked in TCS, Wipro and Cognizant interviews.
- Difficulty: Easy
- Topics: Strings, Hash Table, Math
- Asked at: TCS, Wipro, Cognizant
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A three-digit number n is fascinating if, after concatenating n, 2 * n and 3 * n into one string, the result contains each digit from 1 to 9 exactly once and contains no 0.
Return true if n is fascinating.
Example 1
Input: n = 192
Output: true
Explanation: 192, 384 and 576 concatenate to 192384576, which uses 1 through 9 once each.
Example 2
Input: n = 100
Output: false
Explanation: 100200300 is full of zeros.
Example 3
Input: n = 583
Output: false
Explanation: 583, 1166 and 1749 concatenate to eleven digits, so the count is already wrong.
Constraints
100 <= n <= 999
Hints for Check if The Number is Fascinating
Hint 1
Build the concatenation as a string — that is the cleanest way to inspect the digits.
Hint 2
The result must be exactly nine characters long; anything else fails immediately.
Hint 3
Tally the ten digits:
0must be absent and each of1..9must appear once.
How to solve Check if The Number is Fascinating
The property is stated directly in terms of the digit string, so build it and tally. The length check is a cheap early exit that also guards against 3 * n growing to four digits.
Approach
- Concatenate the decimal forms of
n,2 * nand3 * n. - If the result is not nine characters long, return
false. - Tally the digits; reject if any
0appears or if any digit1..9does not appear exactly once.
Why it works
Nine characters holding the nine non-zero digits once each is exactly the definition, and checking the length first means the tally never has to reason about repeated digits in an over-long string.
Complexity
- Time —
O(1) - Space —
O(1)
Pitfalls
- Skipping the length check lets a four-digit
3 * nslip through with a coincidentally valid tally prefix. - Checking only for duplicates misses a missing digit, and vice versa — both conditions are needed.
Reference solution
Python
def isFascinating(n: int) -> bool:
joined = str(n) + str(2 * n) + str(3 * n)
if len(joined) != 9:
return False
return sorted(joined) == list("123456789")Java
public static boolean isFascinating(int n) {
String joined = "" + n + (2 * n) + (3 * n);
if (joined.length() != 9) return false;
int[] seen = new int[10];
for (int i = 0; i < joined.length(); i++) seen[joined.charAt(i) - '0']++;
if (seen[0] != 0) return false;
for (int d = 1; d <= 9; d++) {
if (seen[d] != 1) return false;
}
return true;
}C++
bool isFascinating(int n) {
string joined = to_string(n) + to_string(2 * n) + to_string(3 * n);
if (joined.size() != 9) return false;
vector<int> seen(10, 0);
for (char c : joined) seen[c - '0']++;
if (seen[0] != 0) return false;
for (int d = 1; d <= 9; d++) {
if (seen[d] != 1) return false;
}
return true;
}JavaScript
var isFascinating = function(n) {
var joined = String(n) + String(2 * n) + String(3 * n);
if (joined.length !== 9) return false;
var seen = [];
for (var t = 0; t < 10; t++) seen.push(0);
for (var i = 0; i < joined.length; i++) seen[joined.charCodeAt(i) - 48]++;
if (seen[0] !== 0) return false;
for (var d = 1; d <= 9; d++) {
if (seen[d] !== 1) return false;
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings · Hashing: Hash Maps and Hash Sets