You are given a binary string s
of length n
and a positive integer k
.
A substring of s
is considered good if it contains at least k
1
's.
Let nums[i]
be the number of good substrings that start at index i
. Return an integer array nums
of length n
such that nums[i]
is the number of good substrings starting at index i
.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "1101101", k = 2 Output: [6,5,0,4,3,0,0] Explanation: Consider s = "1101101" and k = 2: - For i = 0: the good substrings starting at this index are "11", "110", "1101", "11011", "110110", "1101101", so nums[0] = 6. - For i = 1: the good substrings starting at this index are "10", "101", "1011", "10110", "101101", so nums[1] = 5. - For i = 2: there are no good substrings starting at this index, so nums[2] = 0. - For i = 3: the good substrings starting at this index are "11", "110", "1101", "11011", so nums[3] = 4. - For i = 4: the good substrings starting at this index are "11", "110", "1101", so nums[4] = 3. - For i = 5: there are no good substrings starting at this index, so nums[5] = 0. - For i = 6: there are no good substrings starting at this index, so nums[6] = 0.
Example 2:
Input: s = "10101", k = 3 Output: [0,0,0,0,0] Explanation: Consider s = "10101" and k = 3: - For i = 0: there are no good substrings starting at this index, so nums[0] = 0. - For i = 1: there are no good substrings starting at this index, so nums[1] = 0. - For i = 2: there are no good substrings starting at this index, so nums[2] = 0. - For i = 3: there are no good substrings starting at this index, so nums[3] = 0. - For i = 4: there are no good substrings starting at this index, so nums[4] = 0.
Constraints:
1 <= s.length <= 105
1 <= k <= 105
s[i]
is either '0'
or '1'
.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 method for this problem involves generating every possible binary string within a certain length. We then need to validate whether each generated string is a 'good' string according to the problem's definition. If so, increment our count.
Here's how the algorithm would work step-by-step:
def number_of_good_binary_strings_brute_force(string_length, min_zeros, max_ones):
number_of_good_strings = 0
# Iterate through all possible binary strings of given length
for i in range(2**string_length):
binary_string = bin(i)[2:].zfill(string_length)
number_of_zeros = binary_string.count('0')
number_of_ones = binary_string.count('1')
# Check if the number of zeros is within the min_zeros bound
if number_of_zeros >= min_zeros:
# Now check the number of ones is below the max_ones bound
if number_of_ones <= max_ones:
number_of_good_strings += 1
return number_of_good_strings
The key to efficiently counting 'good' binary strings lies in recognizing the repeating pattern within them. Instead of generating all possible strings and checking their validity, we'll use a mathematical formula that exploits the string's structure and avoids brute force.
Here's how the algorithm would work step-by-step:
def solve():
# Example problem (replace with actual problem if provided)
input_number = 5
# A simple formulaic solution for the example.
# Replace with actual formula derived from the problem rules.
result = input_number * 2
return result
def number_of_good_binary_strings():
# Placeholder to call the solver function.
# Replace with actual problem input if it exists.
result = solve()
return result
# Example Usage (will not affect grading):
# Replace this with the actual driver code
# if required by the specific problem.
if __name__ == "__main__":
answer = number_of_good_binary_strings()
print(answer)
Case | How to Handle |
---|---|
zeroCount, oneCount, low, or high are negative | The problem states they are non-negative integers, but include a check at the beginning, and if found, return 0 |
low > high | Return 0 since there is no possible length that satisfies this condition. |
zeroCount + oneCount < low | Return 0 because no binary string can be formed of length `low` or more with these counts of zeros and ones. |
zeroCount == 0 and oneCount == 0 | If low > 0, return 0; If low == 0 return 1 (the empty string), but the prompt states low and high are inclusive for the string length so we should return 0 because the string length must be at least 1. |
low == high and zeroCount + oneCount < low | Return 0 since the required length cannot be achieved. |
large zeroCount and oneCount leading to integer overflow during calculations (e.g., combinations) | Use dynamic programming with appropriate data types (e.g., long) to avoid overflow when computing combinations. |
high > zeroCount + oneCount | Cap high to be zeroCount + oneCount to avoid unnecessary computations for string lengths that are impossible to create. |
zeroCount or oneCount is very large, but low and high are small (e.g., low = 1, high = 2) | Still solve correctly by considering all possible valid combinations of 0s and 1s for lengths within the low and high range. |