LeetCode 3348: Smallest Divisible Digit Product II Solution
Master LeetCode problem 3348 (Smallest Divisible Digit Product II), a hard 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.
3348. Smallest Divisible Digit Product II
Problem Explanation
Explanation:
To solve this problem, we can generate all possible zero-free numbers greater than or equal to the given number num. We can do this by iterating through all numbers starting from num and checking if the product of their digits is divisible by t. We can create a helper function to calculate the product of digits for each number.
Algorithm:
- Initialize a variable to store the integer representation of
num. - Iterate from this integer representation to a maximum limit (which can be set based on the constraints) to find the smallest zero-free number greater than or equal to
num. - For each number, calculate the product of its digits using a helper function.
- If the product is divisible by
t, return the number. - If no such number is found, return
-1.
Time Complexity:
The time complexity of this solution is O(N*M) where N is the length of num and M is the maximum limit for checking numbers.
Space Complexity:
The space complexity is O(1) as we are not using any extra space proportional to the input size.
:
Solution Code
class Solution {
public String getSmallestString(String num, int t) {
long n = Long.parseLong(num);
for (long i = n; i <= 2e5; i++) {
if (isZeroFree(i) && getProductOfDigits(i) % t == 0) {
return Long.toString(i);
}
}
return "-1";
}
private boolean isZeroFree(long num) {
while (num > 0) {
if (num % 10 == 0) {
return false;
}
num /= 10;
}
return true;
}
private long getProductOfDigits(long num) {
long product = 1;
while (num > 0) {
product *= num % 10;
num /= 10;
}
return product;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3348 (Smallest Divisible Digit Product II)?
This page provides optimized solutions for LeetCode problem 3348 (Smallest Divisible Digit Product II) 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 3348 (Smallest Divisible Digit Product II)?
The time complexity for LeetCode 3348 (Smallest Divisible Digit Product II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3348 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3348 in Java, C++, or Python.