Article directory
One [topic category]
- sliding window
Two [question difficulty]
- Simple
Three [topic number]
- 2269. Find the K beauty value of a number
Four [title description]
- The k beauty value of an integer num is defined as the number of substrings in num that satisfy the following conditions:
- The substring is of length k.
- The substring divides num evenly.
- Given the integers num and k, please return the k value of num.
- Notice:
- Prefix 0 is allowed.
- 0 is not divisible by any value.
- A substring is a contiguous sequence of characters in a string.
Five [topic example]
-
Example 1:
- Input: num = 240, k = 2
- Output: 2
- Explanation: The following are substrings of length k in num:
- "24" in "240": 24 can divide 240 evenly.
- "40" in "240": 40 can divide 240 evenly.
- Therefore, the value of kbeauty is 2.
-
Example 2:
- Input: num = 430043, k = 2
- Output: 2
- Explanation: The following are substrings of length k in num:
- "43" in "430043": 43 can divide 430043 evenly.
- "30" in "430043": 30 is not divisible by 430043.
- "00" in "430043": 0 is not divisible by 430043.
- "04" in "430043": 4 is not divisible by 430043.
- "43" in "430043": 43 can divide 430043 evenly.
- Therefore, the value of kbeauty is 2.
Six [topic prompt]
- 1 < = n u m < = 1 0 9 1 <= num <= 10^9 1<=num<=109
- 1 <= k <= num.length (treat num as a string) 1 <= k <= num.length (treat num as a string)1<=k<=n u m . l e n g t h (treat n u m as a string)
Seven [problem-solving ideas]
- Using the idea of sliding windows
- The fixed size of this window is the k given by the question, and it slides from left to right. If the number in this window is not 0 and can divide the given number, then it will be recorded in the result
- You need to be careful not to cross the boundary. The idea is not difficult. If you don’t use library functions, the specific implementation is a little more complicated. You can see the following code for details, which is written in great detail.
- Finally return the result
Eight 【Time Frequency】
- Time complexity: O ( n ) O(n)O(n), n n n is the length of the incoming number
- Space complexity: O ( 1 ) O(1)O(1)
Nine [code implementation]
- Java language version
class Solution {
public int divisorSubstrings(int num, int k) {
int res = 0;
int[] temp_num = new int[10];
int i,count,temp;
for(i = 0,temp = num;temp > 0;temp /= 10,i++){
temp_num[i] = temp % 10;
}
count = i - k + 1;
for(i = 0;i < count;i++){
int sum = 0;
for(int j = 0;j<k;j++){
sum *= 10;
sum += temp_num[i + k - j - 1];
}
if(sum != 0 && num % sum == 0){
res++;
}
}
return res;
}
}
- C language version
int divisorSubstrings(int num, int k)
{
int res = 0;
int* temp_num = (int*)malloc(sizeof(int) * 10);
int i,count,temp;
for(i = 0,temp = num;temp > 0;temp /= 10,i++)
{
temp_num[i] = temp % 10;
}
count = i - k + 1;
for(i = 0;i < count;i++)
{
int sum = 0;
for(int j = 0;j < k;j++)
{
sum *= 10;
sum += temp_num[i + k - j - 1];
}
if(sum != 0 && num % sum == 0)
{
res++;
}
}
return res;
}
- Python language version
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
res = 0
num = str(num)
for i in range(0,len(num) - k + 1):
temp = int(num[i:i+k])
if temp == 0:
continue
if int(num) % temp == 0:
res += 1
return res;
- C++ language version
class Solution {
public:
int divisorSubstrings(int num, int k) {
int res = 0;
int i,count,temp;
int* temp_count = (int*)malloc(sizeof(int) * 10);
for(i = 0,temp = num;temp > 0;temp /= 10,i++){
temp_count[i] = temp % 10;
}
count = i - k + 1;
for(i = 0;i < count;i++){
int sum = 0;
for(int j = 0;j < k;j++){
sum *= 10;
sum += temp_count[i + k - j - 1];
}
if(sum != 0 && num % sum == 0){
res++;
}
}
return res;
}
};
Ten【Submission Results】
-
Java language version
-
C language version
-
Python language version
-
C++ language version