Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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:
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 <= 999Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Math
192
100
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2729: Check if The Number is Fascinating
class Solution {
public boolean isFascinating(int n) {
String s = "" + n + (2 * n) + (3 * n);
int[] cnt = new int[10];
for (char c : s.toCharArray()) {
if (++cnt[c - '0'] > 1) {
return false;
}
}
return cnt[0] == 0 && s.length() == 9;
}
}
// Accepted solution for LeetCode #2729: Check if The Number is Fascinating
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
}
# Accepted solution for LeetCode #2729: Check if The Number is Fascinating
class Solution:
def isFascinating(self, n: int) -> bool:
s = str(n) + str(2 * n) + str(3 * n)
return "".join(sorted(s)) == "123456789"
// Accepted solution for LeetCode #2729: Check if The Number is Fascinating
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
}
}
// Accepted solution for LeetCode #2729: Check if The Number is Fascinating
function isFascinating(n: number): boolean {
const s = `${n}${n * 2}${n * 3}`;
return s.split('').sort().join('') === '123456789';
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.