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:
k
.num
.Given integers num
and k
, return the k-beauty of num
.
Note:
0
is 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 <= 109
1 <= k <= num.length
(taking num
as a string)When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the K-beauty of a number involves checking every possible substring of length K. We go through the number digit by digit, and for each substring, we see if it divides the original number evenly.
Here's how the algorithm would work step-by-step:
def find_k_beauty(number, k_digits):
number_string = str(number)
number_string_length = len(number_string)
beauty_count = 0
for index in range(number_string_length - k_digits + 1):
# Extract substring of length k_digits
substring = number_string[index:index + k_digits]
substring_as_integer = int(substring)
# Avoid division by zero
if substring_as_integer == 0:
continue
# Check if substring divides the number
if number % substring_as_integer == 0:
beauty_count += 1
return beauty_count
The goal is to count how many substrings of a number can evenly divide the original number. Instead of checking every possible substring, we extract and check the divisibility of substrings efficiently to avoid redundant calculations.
Here's how the algorithm would work step-by-step:
def find_k_beauty(number, substring_length):
number_string = str(number)
string_length = len(number_string)
k_beauty_count = 0
# Iterate through all possible substring lengths.
for current_substring_length in range(1, substring_length + 1):
for i in range(string_length - current_substring_length + 1):
substring = number_string[i:i + current_substring_length]
# Convert the substring back to an integer
substring_number = int(substring)
# Avoid division by zero and check divisibility
if substring_number > 0:
if number % substring_number == 0:
# Count if the substring divides evenly into the number
k_beauty_count += 1
return k_beauty_count
Case | How to Handle |
---|---|
k is zero | Return 0 because a 0-digit number is meaningless in this context. |
k is greater than the number of digits in num | Return 0 because no k-digit number can be formed. |
num is zero | If k is 1, return 1, otherwise return 0 as any k-digit number (k>1) cannot be extracted from zero. |
num is negative | Convert num to its absolute value since divisibility is checked regardless of sign. |
k is 1 and num has multiple digits of 0 | The solution correctly identifies each zero digit as a divisor if num is divisible by 0. |
k-digit substring is 0 | Avoid division by zero by checking if the k-digit number is zero before performing the division. |
Integer overflow when extracting k-digit substrings | Use a data type capable of holding large integers, like long, to avoid potential overflow during substring extraction and conversion. |
Large num with a small k (e.g., num = 1234567890, k = 1) | Ensure the solution's time complexity is linear with respect to the number of digits in num to handle large numbers efficiently. |