Skip to main content
Back to problems
Leetcode
Easy
Arrays
Strings
Math
Find The K Beauty Of A Number

Count how many length-kk substrings of a number divide the number itself.

Acceptance 0%
Problem Statement

Given a positive integer num, consider every contiguous substring of its decimal representation with length exactly k. For each such substring, treat it as an integer value (leading zeros are allowed in the substring, but they do not change the numeric value).

A substring is called a k-beauty of num if its numeric value is non-zero and it divides num evenly.

Return the number of k-beauty substrings.

Idea

You only need to inspect all length-kk windows in the digit string of num, convert each window to an integer, and test whether it is a non-zero divisor of num.

Input Format

  • A positive integer num.
  • An integer k.

Treat num as a decimal string when scanning substrings.

Output Format

  • Return a single integer: the number of length-kk substrings whose numeric value is a non-zero divisor of num.

Constraints

  • 1 <= num
  • 1 <= k <= number of digits in num
  • Substrings may contain leading zeros.
  • Ignore any substring whose numeric value is 0.
Examples
Sample cases returned by the problem API.

Example 1

Input

num = 240, k = 2

Output

2

Explanation

The length-2 substrings are 24 and 40. Both divide 240, so the answer is 2.

Example 2

Input

num = 430043, k = 2

Output

2

Explanation

The length-2 substrings are 43, 30, 00, 04, and 43. Only 43 divides 430043, and it appears twice. The 00 and 04 windows are ignored if their numeric value is 0 or not a divisor.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.