Difficulty: Easy | Acceptance: 53.10% | Paid: No Topics: Hash Table, Math
You are given an integer n. A number is fascinating if it concatenates n, 2n, and 3n, and the resulting number contains all digits from 1 to 9 exactly once and does not contain any 0.
Return true if n is fascinating, otherwise return false.
- Examples
- Constraints
- String Concatenation with Set
- Array Counting
- Bit Manipulation
Examples
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
1 <= n <= 100
String Concatenation with Set
Intuition Concatenate n, 2n, and 3n as a string, then use a set to verify each digit from 1-9 appears exactly once with no zeros.
Steps
- Concatenate n, 2n, and 3n into a single string
- If length is not 9, return false
- Iterate through each character, rejecting zeros and duplicates
- Return true if set contains exactly 9 unique digits
class Solution:
def isFascinating(self, n: int) -> bool:
s = str(n) + str(2*n) + str(3*n)
if len(s) != 9:
return False
seen = set()
for c in s:
if c == '0' or c in seen:
return False
seen.add(c)
return len(seen) == 9Complexity
- Time: O(1) - Fixed 9 characters to process
- Space: O(1) - Set holds at most 9 elements
- Notes: Clean and readable, uses hash set for O(1) lookups
Array Counting
Intuition Use a fixed-size array to count digit occurrences, checking that digits 1-9 each appear exactly once.
Steps
- Concatenate n, 2n, and 3n into a string
- Use an array of size 10 to count each digit
- Reject zeros and any digit appearing more than once
- Verify all digits 1-9 have count exactly 1
class Solution:
def isFascinating(self, n: int) -> bool:
s = str(n) + str(2*n) + str(3*n)
if len(s) != 9:
return False
count = [0] * 10
for c in s:
digit = int(c)
if digit == 0:
return False
count[digit] += 1
if count[digit] > 1:
return False
return all(count[i] == 1 for i in range(1, 10))Complexity
- Time: O(1) - Fixed 9 characters to process
- Space: O(1) - Fixed array of size 10
- Notes: More memory efficient than hash set, direct index access
Bit Manipulation
Intuition Use a bitmask to track which digits have been seen, setting bit i when digit i is encountered.
Steps
- Concatenate n, 2n, and 3n into a string
- Use an integer as a bitmask, setting bit d for each digit d
- Reject zeros and any digit already seen (bit already set)
- Check if final mask equals 0b1111111110 (bits 1-9 all set)
class Solution:
def isFascinating(self, n: int) -> bool:
s = str(n) + str(2*n) + str(3*n)
if len(s) != 9:
return False
mask = 0
for c in s:
digit = int(c)
if digit == 0:
return False
bit = 1 << digit
if mask & bit:
return False
mask |= bit
return mask == 0b1111111110Complexity
- Time: O(1) - Fixed 9 characters to process
- Space: O(1) - Single integer for bitmask
- Notes: Most space-efficient, bit operations are very fast