Find the K-Beauty of a Number - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: num = 240, k = 2
Output: 2
💡 Note: Substrings of length 2: "24" and "40". Both 24 and 40 divide 240 evenly, so k-beauty is 2.
Example 2 — With Leading Zero
$ Input: num = 430, k = 2
Output: 2
💡 Note: Substrings: "43", "30". 430 ÷ 43 = 10 and 430 ÷ 30 = 14.33... Only "43" divides evenly, but "30" also works: 430 ÷ 30 = 14.33 (not integer). Actually 43 divides 430, and 30 doesn't divide 430 evenly.
Example 3 — No Valid Divisors
$ Input: num = 123, k = 3
Output: 1
💡 Note: Only substring is "123". Since 123 ÷ 123 = 1, it divides itself, so k-beauty is 1.

Constraints

  • 1 ≤ num ≤ 109
  • 1 ≤ k ≤ num.length

Visualization

Tap to expand
K-Beauty Problem: Count Valid K-Length DivisorsInput: num = 240, k = 22402440240 ÷ 24 = 10 ✓240 ÷ 40 = 6 ✓K-Beauty = 2
Understanding the Visualization
1
Input
Number 240 with k=2
2
Extract Substrings
Get all 2-length substrings: "24", "40"
3
Count Divisors
Count how many substrings divide the original number
Key Takeaway
🎯 Key Insight: Convert to string, slide window of size k, and count valid divisors
Asked in
Amazon 15 Google 12
12.5K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen