You are given a string num
representing a positive integer and an integer k
. The k-beauty
of num
is defined as the number of substrings of num
of length k
that meet the following criteria:
k
.Find and return the k-beauty
of num
.
num
and k
?
num
have up to 10^6
digits, and is k
in the range of [1, |num|]
?k > |num|
?num
has leading zeros?#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int divisorSubstrings(string num, int k) {
int count = 0;
int n = num.size();
for (int i = 0; i <= n - k; i++) {
string substring = num.substr(i, k);
int val = stoi(substring);
if (val > 0 && stoi(num) % val == 0) {
count++;
}
}
return count;
}
};
int main() {
Solution sol;
string num = "240"; // Example input (Adjust as needed)
int k = 2; // Example input (Adjust as needed)
cout << sol.divisorSubstrings(num, k) << endl; // Output: 2 (depends on example)
return 0;
}
num
and extract every substring of length k
.num
.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?