Problem Description
Given an integer n with exactly 3 digits, determine if n is fascinating. A number is fascinating if, when you concatenate n, 2n, and 3n into one number, the resulting number contains every digit from 1 to 9 exactly once and does not contain any zeros.
Key Insights
- n is a 3-digit number, so n, 2n, and 3n together will ideally form a 9-digit number.
- Using concatenation, the resulting string must be checked for:
- Having a total length of 9 digits.
- Containing all digits from 1 to 9 exactly once.
- Not containing the digit 0.
- A straightforward approach is to generate the concatenated string and then verify it against "123456789".
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
The solution involves first creating the concatenated string by converting n, 2n, and 3n to strings and joining them. Since the length is fixed (9 characters), we simply need to check if sorting this string results in "123456789". This also ensures that each digit is present exactly once and that there are no zeros.