Count how many length- substrings of a number divide the number itself.
Given a positive integer num, consider every contiguous substring of its decimal representation with length exactly k. For each such substring, treat it as an integer value (leading zeros are allowed in the substring, but they do not change the numeric value).
A substring is called a k-beauty of num if its numeric value is non-zero and it divides num evenly.
Return the number of k-beauty substrings.
Idea
You only need to inspect all length- windows in the digit string of num, convert each window to an integer, and test whether it is a non-zero divisor of num.
Input Format
- A positive integer
num. - An integer
k.
Treat num as a decimal string when scanning substrings.
Output Format
- Return a single integer: the number of length- substrings whose numeric value is a non-zero divisor of
num.
Constraints
1 <= num1 <= k <=number of digits innum- Substrings may contain leading zeros.
- Ignore any substring whose numeric value is
0.
Example 1
Input
num = 240, k = 2
Output
2
Explanation
The length-2 substrings are 24 and 40. Both divide 240, so the answer is 2.
Example 2
Input
num = 430043, k = 2
Output
2
Explanation
The length-2 substrings are 43, 30, 00, 04, and 43. Only 43 divides 430043, and it appears twice. The 00 and 04 windows are ignored if their numeric value is 0 or not a divisor.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.