Date: 2026-06-06
Time: 16:47
find-the-k-beauty-of-a-number/solution.pyThis file solves LeetCode 2269: Find the K-Beauty of a Number. It implements a single function divisor_substrings that counts how many contiguous length-k substrings of a number's decimal representation evenly divide that number. It's one of ~500 solutions in the leetcode-implementations repo, each isolated in its own directory with a solution, tests, plan, and review.
divisor_substrings(num: int, k: int) -> int — The sole public function. Contract: given a positive integer num and a substring length k, returns the count of length-k contiguous substrings of num (read as a decimal string) that are nonzero and divide num evenly.
Sliding window over string representation. The solution converts num to a string, then slides a window of size k across it, converting each substring back to an integer. This is the standard idiom for digit-substring problems — it avoids modular arithmetic entirely and reads clearly.
The loop for i in range(len(s) - k + 1) generates exactly the right number of windows: for a string of length n, there are n - k + 1 substrings of length k.
Imports: None. Pure stdlib Python with no external dependencies.
Imported by: find-the-k-beauty-of-a-number/testsolution.py imports divisorsubstrings for testing. The massive "Imported By" list in the prompt is an artifact of the repo's test infrastructure — those test files likely share a common harness or conftest, not direct imports of this function.
1. str(num) — convert integer to its decimal string representation.
2. Iterate i from 0 to len(s) - k inclusive — each i is the start of a length-k window.
3. int(s[i:i+k]) — extract the substring and convert back to integer. Leading zeros naturally collapse (e.g., "03" becomes 3).
4. Guard sub != 0 — skip zero-valued substrings to avoid division by zero.
5. Check num % sub == 0 — the divisibility test.
6. Increment count on match; return final count.
"00" parse to 0; the sub != 0 check prevents ZeroDivisionError. This is the only validation the function performs.k exceeds the digit count, range(len(s) - k + 1) produces an empty range and the function returns 0 — safe but vacuously so.None. The function trusts its inputs per LeetCode problem constraints (1 ≤ k ≤ len(str(num)), num ≥ 1). The only defensive check is the zero-substring guard, which is part of the problem's logic rather than error handling.