You are given a binary string s consisting only of zeroes and ones.
A substring of s is considered balanced if all zeroes are before ones and the number of zeroes is equal to the number of ones inside the substring. Notice that the empty substring is considered a balanced substring.
Return the length of the longest balanced substring of s.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "01000111" Output: 6 Explanation: The longest balanced substring is "000111", which has length 6.
Example 2:
Input: s = "00111" Output: 4 Explanation: The longest balanced substring is "0011", which has length 4.
Example 3:
Input: s = "111" Output: 0 Explanation: There is no balanced substring except the empty substring, so the answer is 0.
Constraints:
1 <= s.length <= 50'0' <= s[i] <= '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 approach checks every possible part of the binary string to see if it's balanced. We consider all possible substrings, one by one, to find the longest one that satisfies the balance requirement. We are trying everything to find the answer.
Here's how the algorithm would work step-by-step:
def find_longest_balanced_substring_brute_force(binary_string):
longest_balanced_substring_length = 0
string_length = len(binary_string)
for start_index in range(string_length):
for end_index in range(start_index, string_length):
substring = binary_string[start_index:end_index + 1]
substring_length = len(substring)
zeros_count = 0
ones_count = 0
for char in substring:
if char == '0':
zeros_count += 1
else:
ones_count += 1
# Check if the current substring is balanced.
if zeros_count == ones_count:
# Update max length if current substring is longer
if substring_length > longest_balanced_substring_length:
longest_balanced_substring_length = substring_length
return longest_balanced_substring_lengthThe problem asks us to find the longest part of a string that has an equal number of zeros and ones. Instead of checking every possible part, we can cleverly track how far we are from having a balanced substring, and quickly identify when a longer balanced substring appears by remembering where we've seen certain patterns before.
Here's how the algorithm would work step-by-step:
def find_longest_balanced_substring(binary_string):
counter = 0
first_occurrence = {0: -1}
max_length = 0
for i, bit in enumerate(binary_string):
if bit == '0':
counter += 1
else:
counter -= 1
# Check if this counter value has been seen before
if counter in first_occurrence:
current_length = i - first_occurrence[counter]
max_length = max(max_length, current_length)
# Store first index of current count if not already stored
else:
first_occurrence[counter] = i
return max_length| Case | How to Handle |
|---|---|
| Empty string input | Return 0 immediately as an empty string has no balanced substring. |
| String with only one character | Return 0 since a single character cannot form a balanced substring. |
| String with all '0's | Return 0 as there are no '1's to balance the '0's. |
| String with all '1's | Return 0 as there are no '0's to balance the '1's. |
| String with alternating '0's and '1's (e.g., '010101') | The longest balanced substring will be the entire string if the counts are equal or twice the minimum count. |
| String with highly skewed distribution (e.g., many '0's followed by a few '1's) | The solution should correctly identify the longest balanced substring regardless of the distribution. |
| Very long string (potential performance bottleneck) | Ensure the algorithm has a time complexity of O(n) or O(n log n) to handle large inputs efficiently; avoid quadratic time complexity. |
| String with leading or trailing unbalanced characters | The algorithm should correctly identify the longest balanced substring within the string, ignoring any leading or trailing unbalanced characters. |