Given a string s
consisting of digits from '1'
to '9'
and a positive integer k
, find the number of non-empty substrings of s
such that the number it represents is divisible by k
.
A substring is a contiguous sequence of characters within a string.
Since the answer may be very large, return it modulo 109 + 7
.
Example 1:
Input: s = "453", k = 3
Output: 4
Explanation: The substrings of s that are divisible by 3 are "453", "45", "53", and "3".
Example 2:
Input: s = "12", k = 2
Output: 2
Explanation: The substrings of s that are divisible by 2 are "12", and "2".
Example 3:
Input: s = "1234", k = 5
Output: 0
Explanation: There are no substrings of s that are divisible by 5.
Constraints:
1 <= s.length <= 3 * 104
s[i]
is a digit from '1'
to '9'
.1 <= k <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is simple: we will look at every possible substring within the given number string. For each substring, we will check if it meets the divisibility requirement.
Here's how the algorithm would work step-by-step:
def number_of_divisible_substrings_brute_force(number, divisor):
number_string = str(number)
string_length = len(number_string)
divisible_substring_count = 0
for substring_start_index in range(string_length):
for substring_end_index in range(substring_start_index, string_length):
# Extract substring from start to end indices.
substring = number_string[substring_start_index : substring_end_index + 1]
# Convert substring to an integer.
substring_integer = int(substring)
# Check if the substring is divisible by the divisor.
# We check divisibility here.
if substring_integer % divisor == 0:
divisible_substring_count += 1
# The result is the count of divisible substrings.
return divisible_substring_count
The core idea is to efficiently count the substrings divisible by a certain number without exhaustively checking every substring. We can achieve this by focusing on the remainders of the running sums of digits as we traverse the string.
Here's how the algorithm would work step-by-step:
def number_of_divisible_substrings(number_string, divisor):
string_length = len(number_string)
divisible_substring_count = 0
for starting_index in range(string_length):
running_total = 0
for ending_index in range(starting_index, string_length):
current_digit = int(number_string[ending_index])
running_total = (running_total * 10 + current_digit)
# Modulo operation keeps numbers manageable.
running_total_modulo = running_total % divisor
if running_total_modulo == 0:
# Divisible substring found.
divisible_substring_count += 1
return divisible_substring_count
Case | How to Handle |
---|---|
Null or empty input string num | Return 0 if num is null or empty, as there are no substrings to evaluate. |
divisor is 0 | Return 0 to avoid division by zero errors. |
Single-character string '0' and divisor is not 0 | Return 1 if the divisor is not zero, and the single character is '0'. |
String contains leading zeros e.g. '0012' | Consider substrings starting with zeros as potentially valid integers when checking divisibility. |
Large input string length causing integer overflow when converting substrings to integers | Use modulo operator (%) to avoid integer overflow during calculation. |
divisor is a negative number | Take the absolute value of the divisor to deal with negative divisors consistently. |
No divisible substrings exist | The solution should correctly return 0 when no substrings are divisible by the divisor. |
Very large divisor and short substrings | Ensure substrings are parsed and evaluated correctly even when the divisor is significantly larger than the substring's integer value. |