Skip to content

Commit 24f2254

Browse files
authored
Create find-the-k-beauty-of-a-number.cpp
1 parent d3bead3 commit 24f2254

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/find-the-k-beauty-of-a-number.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(logn)
2+
// Space: O(logn)
3+
4+
// sliding window
5+
class Solution {
6+
public:
7+
int divisorSubstrings(int num, int k) {
8+
int result = 0;
9+
string s = to_string(num);
10+
int base = pow(10, k - 1);
11+
for (int i = 0, curr = 0; i < size(s); ++i) {
12+
if (i - k >= 0) {
13+
curr -= (s[i - k] - '0') * base;
14+
}
15+
curr = curr * 10 + (s[i] - '0');
16+
if (i + 1 >= k) {
17+
result += static_cast<int>(curr && num % curr == 0);
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)