#2269
Easy Algorithms Find the k beauty of a number
Math String Sliding Window
63.1% acceptance
Feb 25, 2026
733
47
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn divisor_substrings(num: i32, k: i32) -> i32 {
let s = num.to_string();
let k = k as usize;
let mut count = 0;
for i in 0..=s.len()-k {
let sub: i32 = s[i..i+k].parse().unwrap();
if sub != 0 && num % sub == 0 {
count += 1;
}
}
count
}
}