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

1
class Solution {
2
public:
3
int divisorSubstrings(int num, int k) {
4
string str = to_string(num);
5
int i = 0, j = 0, n = str.length();
6
int ind = 0;
7

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

20
// shift the window by ++j;
21
// remove previous calculation by ++i
22
++i;
23
++j;
24
}
25
}
26

27
return ind;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0