[LeetCode] Find the K-Beauty of a Number

2269. Find the K-Beauty of a Number

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.
  • 0 is not a divisor of any value.

A substring is a contiguous sequence of characters in a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int divisorSubstrings(int num, int k) {
string n = to_string(num);
int res = 0;
for(int i = 0; i <= n.length() - k; i++) {
int div = stoi(n.substr(i, k));
if(div and num % div == 0) {
res++;
}
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/15/PS/LeetCode/find-the-k-beauty-of-a-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.