{x}
blog image

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 "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 <= 109
  • 1 <= k <= num.length (taking num as a string)

Solution Explanation for Finding the K-Beauty of a Number

This problem asks us to find the number of substrings of length k within the input number num that are also divisors of num. We'll explore two approaches: enumeration and a sliding window technique.

Approach 1: Enumeration

This approach is straightforward:

  1. Convert to String: Convert the input integer num into a string s. This allows easy substring extraction.
  2. Iterate Through Substrings: Iterate through all possible substrings of length k within s.
  3. Check Divisibility: For each substring, convert it back to an integer t. Check if t is a divisor of num (i.e., num % t == 0) and if t is not zero.
  4. Count Divisors: Increment a counter ans if the substring is a divisor.
  5. Return Count: Finally, return the value of ans.

Time Complexity: O(n*k), where n is the number of digits in num. The nested loops (iterating through substrings and performing the modulo operation) dominate the runtime.

Space Complexity: O(log n), primarily due to the string representation of num.

Approach 2: Sliding Window

This approach is more efficient:

  1. Initial Window: Extract the first k digits of num to form the initial sliding window. This can be done efficiently using integer division and modulo operations.
  2. Iterate and Slide: Slide the window one digit at a time. Instead of creating new substrings repeatedly, we update the current window value by removing the leftmost digit and adding the new rightmost digit. This is done using clever arithmetic.
  3. Check Divisibility: After each slide, check if the current window value is a divisor of num.
  4. Count Divisors: Increment ans if it's a divisor.
  5. Return Count: Return ans.

Time Complexity: O(log n), because we iterate through the digits of num only once.

Space Complexity: O(1), because we use only a few integer variables to maintain the sliding window and other data. No large data structures are used.

Code Examples (Python)

Approach 1: Enumeration

def divisorSubstrings(num: int, k: int) -> int:
    s = str(num)
    ans = 0
    for i in range(len(s) - k + 1):
        substring = s[i:i + k]
        t = int(substring)
        if t != 0 and num % t == 0:
            ans += 1
    return ans
 

Approach 2: Sliding Window

def divisorSubstrings_sliding_window(num: int, k: int) -> int:
    s = str(num)
    n = len(s)
    ans = 0
    
    #Handle leading zeros
    if k > n:
        return 0
 
    current_num = int(s[:k])
    if current_num != 0 and num % current_num == 0:
        ans += 1
 
    power_of_10 = 10**(k-1)
 
    for i in range(k,n):
        current_num = (current_num - int(s[i-k]) * power_of_10) * 10 + int(s[i])
        if current_num != 0 and num % current_num == 0:
            ans += 1
    return ans
 

The sliding window approach is significantly more efficient for larger input numbers. The code examples above provide clear implementations of both approaches in Python. Other languages like Java, C++, Go, and TypeScript would have similar implementations, adapting syntax accordingly.