You are given a binary string s
.
Return the number of substrings with dominant ones.
A string has dominant ones if the number of ones in the string is greater than or equal to the square of the number of zeros in the string.
Example 1:
s = "00011"
Output: 5
Explanation:
The substrings with dominant ones are shown in the table below.
i | j | s[i..j] | Number of Zeros | Number of Ones |
---|---|---|---|---|
3 | 3 | 1 | 0 | 1 |
4 | 4 | 1 | 0 | 1 |
2 | 3 | 01 | 1 | 1 |
3 | 4 | 11 | 0 | 2 |
2 | 4 | 011 | 1 | 2 |
Example 2:
s = "101101"
Output: 16
Explanation:
The substrings with non-dominant ones are shown in the table below.
Since there are 21 substrings total and 5 of them have non-dominant ones, it follows that there are 16 substrings with dominant ones.
i | j | s[i..j] | Number of Zeros | Number of Ones |
---|---|---|---|---|
1 | 1 | 0 | 1 | 0 |
4 | 4 | 0 | 1 | 0 |
1 | 4 | 0110 | 2 | 2 |
0 | 4 | 10110 | 2 | 3 |
1 | 5 | 01101 | 2 | 3 |
Constraints:
1 <= s.length <= 4 * 10^4
s
consists only of characters '0'
and '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:
We want to find all sections within a sequence where there are more ones than zeros. The brute force way checks every single possible section to see if it meets the criteria.
Here's how the algorithm would work step-by-step:
def count_substrings_with_dominant_ones(sequence):
total_substrings_with_more_ones = 0
for starting_index in range(len(sequence)):
for ending_index in range(starting_index, len(sequence)):
# Extract the current substring for evaluation.
current_substring = sequence[starting_index:ending_index + 1]
number_of_ones = 0
number_of_zeros = 0
for bit in current_substring:
if bit == '1':
number_of_ones += 1
else:
number_of_zeros += 1
# Determine if the substring has more ones than zeros
if number_of_ones > number_of_zeros:
# Count substrings that satisfy the condition.
total_substrings_with_more_ones += 1
# Returns the final count
return total_substrings_with_more_ones
The most efficient approach focuses on tracking groups of consecutive ones and leveraging that information to count valid substrings quickly. By avoiding redundant calculations, we can determine the number of substrings with more ones than zeros in a single pass.
Here's how the algorithm would work step-by-step:
def count_substrings_with_dominant_ones(binary_string):
string_length = len(binary_string)
total_valid_substrings = 0
index = 0
while index < string_length:
if binary_string[index] == '1':
ones_group_length = 0
group_index = index
# Find the length of the consecutive ones group
while group_index < string_length and binary_string[group_index] == '1':
ones_group_length += 1
group_index += 1
# Calculate the number of zeros needed to be less than ones
zeros_needed = ones_group_length
left_index = index - 1
zeros_count = 0
# Determine how far left to go to form valid substring
while left_index >= 0 and zeros_count < zeros_needed:
if binary_string[left_index] == '0':
zeros_count += 1
left_index -= 1
#This is the number of valid substrings ending at this group.
total_valid_substrings += (index - left_index - 1)
index = group_index
else:
index += 1
return total_valid_substrings
Case | How to Handle |
---|---|
Null or empty string input | Return 0, as there are no substrings to evaluate. |
String of length 1 or 2 | Check if the single character (or each character) is '1', and increment the count if it is and there are more 1s. |
String containing only zeros (e.g., '0000') | Return 0, as no substring can have more ones than zeros. |
String containing only ones (e.g., '1111') | Return n*(n+1)/2, as every substring will have more ones than zeros. |
String with alternating ones and zeros (e.g., '101010') | Iterate through all substrings, counting ones and zeros and only incrementing count if ones > zeros. |
Very long string approaching memory limits | The solution should have O(n^2) complexity, and will require enough memory to store string, handle overflow, consider an algorithm for substring validation that does not create a new string for each substring. |
String with integer overflow potential for counting ones or zeros. | Ensure the counting variables (number of ones and number of zeros) are large enough to hold the maximum count or use modulo operation. |
String consists only of 1's at the start and the rest are 0's | The algorithm needs to traverse through each substring even with diminishing '1's to find valid combinations. |