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 <= 109
1 <= k <= num.length
(taking num
as a string)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.
This approach is straightforward:
num
into a string s
. This allows easy substring extraction.k
within s
.t
. Check if t
is a divisor of num
(i.e., num % t == 0
) and if t
is not zero.ans
if the substring is a divisor.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
.
This approach is more efficient:
k
digits of num
to form the initial sliding window. This can be done efficiently using integer division and modulo operations.num
.ans
if it's a divisor.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.
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.