2729. Check if The Number is Fascinating LeetCode Solution

In this guide, you will get 2729. Check if The Number is Fascinating LeetCode Solution with the best time and space complexity. The solution to Check if The Number is Fascinating problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check if The Number is Fascinating solution in C++
  4. Check if The Number is Fascinating solution in Java
  5. Check if The Number is Fascinating solution in Python
  6. Additional Resources
2729. Check if The Number is Fascinating LeetCode Solution image

Problem Statement of Check if The Number is Fascinating

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.

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 <= 999

See also  214. Shortest Palindrome LeetCode Solution

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2729. Check if The Number is Fascinating LeetCode Solution in C++

class Solution {
 public:
  bool isFascinating(int n) {
    string s = to_string(n) + to_string(2 * n) + to_string(3 * n);
    ranges::sort(s);
    return s == "123456789";
  }
};
/* code provided by PROGIEZ */

2729. Check if The Number is Fascinating LeetCode Solution in Java

class Solution {
  public boolean isFascinating(int n) {
    String s = Integer.toString(n) + Integer.toString(2 * n) + Integer.toString(3 * n);
    char[] charArray = s.toCharArray();
    Arrays.sort(charArray);
    return new String(charArray).equals("123456789");
  }
}
// code provided by PROGIEZ

2729. Check if The Number is Fascinating LeetCode Solution in Python

class Solution:
  def isFascinating(self, n):
    s = str(n) + str(2 * n) + str(3 * n)
    return ''.join(sorted(s)) == '123456789'
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.