LeetCode 2729: Check if The Number is Fascinating Solution
Master LeetCode problem 2729 (Check if The Number is Fascinating), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2729. Check if The Number is Fascinating
Problem Explanation
Explanation:
To solve this problem, we need to concatenate the number n with 2n and 3n, then check if the resulting concatenated number contains all digits from 1 to 9 exactly once and does not have any zeros.
- Create a concatenated number by appending n, 2n, and 3n.
- Check if the concatenated number contains all digits from 1 to 9 and no zeros.
- Return true if the conditions are satisfied, false otherwise.
Time Complexity: O(1) Space Complexity: O(1)
Solution Code
class Solution {
public boolean isFascinating(int n) {
String concatenated = n + String.valueOf(2*n) + String.valueOf(3*n);
if (concatenated.length() != 9) {
return false;
}
int[] countDigits = new int[10];
for (char c : concatenated.toCharArray()) {
if (c == '0') {
return false;
}
countDigits[c - '0']++;
}
for (int i = 1; i <= 9; i++) {
if (countDigits[i] != 1) {
return false;
}
}
return true;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2729 (Check if The Number is Fascinating)?
This page provides optimized solutions for LeetCode problem 2729 (Check if The Number is Fascinating) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2729 (Check if The Number is Fascinating)?
The time complexity for LeetCode 2729 (Check if The Number is Fascinating) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2729 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2729 in Java, C++, or Python.