Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
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:
k.num.Given integers num and k, return the k-beauty of num.
Note:
0 is not a divisor of any value.A substring is a contiguous sequence of characters in a string.
Example 1:
Input: num = 240, k = 2 Output: 2 Explanation: The following are the substrings of num of length k: - "24" from "240": 24 is a divisor of 240. - "40" from "240": 40 is a divisor of 240. Therefore, the k-beauty is 2.
Example 2:
Input: num = 430043, k = 2 Output: 2 Explanation: The following are the substrings of num of length k: - "43" from "430043": 43 is a divisor of 430043. - "30" from "430043": 30 is not a divisor of 430043. - "00" from "430043": 0 is not a divisor of 430043. - "04" from "430043": 4 is not a divisor of 430043. - "43" from "430043": 43 is a divisor of 430043. Therefore, the k-beauty is 2.
Constraints:
1 <= num <= 1091 <= k <= num.length (taking num as a string)Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Sliding Window
240 2
430043 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2269: Find the K-Beauty of a Number
class Solution {
public int divisorSubstrings(int num, int k) {
int ans = 0;
String s = "" + num;
for (int i = 0; i < s.length() - k + 1; ++i) {
int t = Integer.parseInt(s.substring(i, i + k));
if (t != 0 && num % t == 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2269: Find the K-Beauty of a Number
func divisorSubstrings(num int, k int) int {
ans := 0
s := strconv.Itoa(num)
for i := 0; i < len(s)-k+1; i++ {
t, _ := strconv.Atoi(s[i : i+k])
if t > 0 && num%t == 0 {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #2269: Find the K-Beauty of a Number
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
ans = 0
s = str(num)
for i in range(len(s) - k + 1):
t = int(s[i : i + k])
if t and num % t == 0:
ans += 1
return ans
// Accepted solution for LeetCode #2269: 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.
*
* Example 1:
*
* Input: num = 240, k = 2
* Output: 2
* Explanation: The following are the substrings of num of length k:
* - "24" from "<u>24</u>0": 24 is a divisor of 240.
* - "40" from "2<u>40</u>": 40 is a divisor of 240.
* Therefore, the k-beauty is 2.
*
* Example 2:
*
* Input: num = 430043, k = 2
* Output: 2
* Explanation: The following are the substrings of num of length k:
* - "43" from "<u>43</u>0043": 43 is a divisor of 430043.
* - "30" from "4<u>30</u>043": 30 is not a divisor of 430043.
* - "00" from "43<u>00</u>43": 0 is not a divisor of 430043.
* - "04" from "430<u>04</u>3": 4 is not a divisor of 430043.
* - "43" from "4300<u>43</u>": 43 is a divisor of 430043.
* Therefore, the k-beauty is 2.
*
*
* Constraints:
*
* 1 <= num <= 10^9
* 1 <= k <= num.length (taking num as a string)
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/find-the-k-beauty-of-a-number/
// discuss: https://leetcode.com/problems/find-the-k-beauty-of-a-number/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn divisor_substrings(num: i32, k: i32) -> i32 {
let c = format!("{num}");
(0..c.len())
.filter_map(|i| {
let d = c.get(i..i + k as usize)?.parse::<i32>().unwrap();
if d != 0 && num % d == 0 {
Some(())
} else {
None
}
})
.count() as _
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2269_example_1() {
let num = 240;
let k = 2;
let result = 2;
assert_eq!(Solution::divisor_substrings(num, k), result);
}
#[test]
fn test_2269_example_2() {
let num = 430043;
let k = 2;
let result = 2;
assert_eq!(Solution::divisor_substrings(num, k), result);
}
}
// Accepted solution for LeetCode #2269: Find the K-Beauty of a Number
function divisorSubstrings(num: number, k: number): number {
let ans = 0;
const s = num.toString();
for (let i = 0; i < s.length - k + 1; ++i) {
const t = parseInt(s.substring(i, i + k));
if (t !== 0 && num % t === 0) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.