Find the K-Beauty of a Number
EASYDescription
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:
- Leading zeros are allowed.
0is not a divisor of any value.
A 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 <= 1091 <= k <= num.length(takingnumas a string)
Approaches
Checkout 2 different approaches to solve Find the K-Beauty of a Number. Click on different approaches to view the approach and algorithm in detail.
Naive String Substring Iteration
This is the most straightforward approach. It converts the number to a string and then iterates through all possible starting positions to extract substrings of length k. Each substring is then converted back to an integer to check for the divisibility condition.
Algorithm
- Convert the input integer
numto its string representation,s. - Initialize a counter,
kBeautyCount, to zero. - Loop through the string
sfrom the first character up to the character at indexs.length() - k. Let the loop variable bei. - Inside the loop, extract the substring of length
kstarting at indexiusings.substring(i, i + k). - Convert this substring into an integer, let's call it
subNumber. - Check if
subNumberis not zero. - If
subNumberis not zero, perform a modulo operation:num % subNumber. If the result is 0, it meanssubNumberis a divisor ofnum. - If
subNumberis a non-zero divisor, incrementkBeautyCount. - After the loop finishes, return
kBeautyCount.
The algorithm works by first converting the number num into a string s to easily access its digits. It then iterates from the beginning of the string to the last possible starting point for a substring of length k. In each iteration, it extracts the k-length substring, converts it to an integer, and checks if this integer is a non-zero divisor of the original number num. A counter is maintained to keep track of how many such substrings are found, which is the final k-beauty value.
class Solution {
public int divisorSubstrings(int num, int k) {
String s = Integer.toString(num);
int n = s.length();
int count = 0;
if (k > n) {
return 0;
}
for (int i = 0; i <= n - k; i++) {
// Extract substring of length k
String subStr = s.substring(i, i + k);
// Convert substring to integer
int subNum = Integer.parseInt(subStr);
// Check for k-beauty conditions
if (subNum != 0 && num % subNum == 0) {
count++;
}
}
return count;
}
}
Complexity Analysis
Pros and Cons
- Very simple and intuitive to write and understand.
- Directly translates the problem statement into code.
- Less efficient due to repeated creation and parsing of substrings. For each step of the iteration, it re-parses
k-1digits that were already part of the previous substring.
Code Solutions
Checking out 3 solutions in different languages for Find the K-Beauty of a Number. Click on different languages to view the code.
class Solution {
public
int divisorSubstrings(int num, int k) {
int ans = 0;
String s = "" + num;
for (int i = 0; i < s.length() - k + 1; ++i) {
int t = Integer.parseInt(s.substring(i, i + k));
if (t != 0 && num % t == 0) {
++ans;
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Find the K-Beauty of a Number
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.