LeetCode 2269: Find the K-Beauty of a Number Solution
Master LeetCode problem 2269 (Find the K-Beauty of a Number), 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.
2269. Find the K-Beauty of a Number
Problem Explanation
Explanation
To solve this problem, we can iterate through all possible substrings of length k of the given number num. For each substring, we check if it is a divisor of num. If it is a divisor, we increment a counter. Finally, we return the counter as the k-beauty of the number.
Time Complexity
The time complexity of this approach is O(n * k), where n is the length of the number num and k is the given length of the substrings.
Space Complexity
The space complexity is O(1) as we are not using any extra space that grows with the input size.
Solution Code
class Solution {
public int beauty(int num, int k) {
String numStr = String.valueOf(num);
int count = 0;
for (int i = 0; i <= numStr.length() - k; i++) {
String sub = numStr.substring(i, i + k);
int subNum = Integer.parseInt(sub);
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2269 (Find the K-Beauty of a Number)?
This page provides optimized solutions for LeetCode problem 2269 (Find the K-Beauty of a Number) 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 2269 (Find the K-Beauty of a Number)?
The time complexity for LeetCode 2269 (Find the K-Beauty of a Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2269 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2269 in Java, C++, or Python.