304 North Cardinal St.
Dorchester Center, MA 02124

# Find the K-Beauty of a Number LeetCode Solution

## Problem – Find the K-Beauty of a Number LeetCode Solution

The k-beauty of an integer `num` is defined as the number of substrings of `num` when it is read as a string that meet the following conditions:

• It has a length of `k`.
• It is a divisor of `num`.

Given integers `num` and `k`, return the k-beauty of `num`.

Note:

• `0` is not a divisor of any value.

substring is a contiguous sequence of characters in a string.

Example 1:

``````Input: num = 240, k = 2
Output: 2
Explanation: The following are the substrings of num of length k:
- "24" from "240": 24 is a divisor of 240.
- "40" from "240": 40 is a divisor of 240.
Therefore, the k-beauty is 2.
``````

Example 2:

``````Input: num = 430043, k = 2
Output: 2
Explanation: The following are the substrings of num of length k:
- "43" from "430043": 43 is a divisor of 430043.
- "30" from "430043": 30 is not a divisor of 430043.
- "00" from "430043": 0 is not a divisor of 430043.
- "04" from "430043": 4 is not a divisor of 430043.
- "43" from "430043": 43 is a divisor of 430043.
Therefore, the k-beauty is 2.
``````

Constraints:

• `1 <= num <= 109`
• `1 <= k <= num.length` (taking `num` as a string)

### Find the K-Beauty of a Number LeetCode Solution in C++

``````class Solution {
public:
int divisorSubstrings(int num, int k) {
string str = to_string(num);
int i = 0, j = 0, n = str.length();
int ind = 0;

while(j < n)
{
if(j - i + 1 < k)
{
// increment j till we get the window size
++j;
}
else if(j - i + 1 == k)
{
// on hiting the window size
// extract window string and convert to int
// check if it follows the given condition
string s = str.substr(i,k);
int n = stoi(s);
if(n != 0 && num % n == 0 )
++ind;

// shift the window by ++j;
// remove previous calculation by ++i
++i;
++j;
}

}

return ind;
}
};
``````

### Find the K-Beauty of a Number LeetCode Solution in Java

``````class Solution {
public int divisorSubstrings(int num, int k) {
String str=String.valueOf(num); // to covert integer to String
int count=0;   // count of ans..
for(int i=0;i<str.length()-k+1;i++)  // deciding the starting index of window
{
String temp=str.substring(i,i+k);    // storing string till window length
int n1=Integer.valueOf(temp);       // converting string to integer
if(n1==0)     // to avoid division error
{
continue;
}
if(num%n1==0)      // if it is divisible then increase the count
{
count++;
}
}
return count;  // lastly return our count
}
}
``````

### Find the K-Beauty of a Number LeetCode Solution in Python

``````def divisorSubstrings(self, num: int, k: int) -> int:
l = 0
r = k

num = str(num)
count = 0
while r <= len(num):
n = int(num[l: r])

# handle case where n could be '0'.
if not n:
l += 1
r += 1
continue

if int(num) % n == 0:
count += 1

# slide window
l += 1
r += 1

return count
``````
##### Find the K-Beauty of a Number LeetCode Solution Review:

In our experience, we suggest you solve this Find the K-Beauty of a Number and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Find the K-Beauty of a Number LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Find the K-Beauty of a Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions