Minimize Removal from Front or End to Make the Binary String at Equilibrium


In this problem, we will print the minimum number of characters that we need to remove from the start and end to make the count of ‘1’ and ‘0’ the same in the given string.

If we find the largest substring with an equal number of ‘1’ and ‘0’, we can get the answer by subtracting the substring length from the given string’s length. We will use the prefix sum technique to find the largest substring having an equal number of ‘1’ and ‘0’.

Problem statement − We have given the ‘bin_str’ binary string containing the N characters. We need to remove some characters from the start and end to make a number of ‘1’ and ‘0’ equal.

Sample examples

Input

bin_str = "0011001"

Output

1

Explanation − We can remove the first ‘0’ to make the count of ‘1’ and ‘0’ equal in the given string.

Input

bin_str = "111000";

Output

0

Explanation − The string already contains the same number of ‘1’ and ‘0’. So, we don’t need to remove any character from the start or end.

Input

bin_str = "00000";

Output

5

Explanation − The string contains the zero ‘1’. So, we need to remove all characters of the given string.

Approach 1

In this approach, we will find the largest substring having an equal number of ‘1’ and ‘0’ in the given binary string.

To simplify the solution code, we will consider the ‘0’ as −1, and ‘1’ as 1. After that, we will use the prefix sum technique to find the largest substring with a prefix sum equal to zero, which is also equal to the largest substring with the same count of ‘1’ and ‘0’.

Algorithm

Step 1 − Define the ‘prefmap’ to store the prefix sum and the index where it occurred.

Step 2 − Initialize the ‘prefsum’ with 0 to track the prefix sum and ‘ans’ to store the maximum length of the substring.

Step 3 − Traverse the binary string. If the current character is ‘0’, add −1 to ‘prefSum’. Otherwise, add ‘1’ to ‘prefSum’.

Step 4 − If the ‘prefSum’ value is 0, update the ‘ans’ value if the current index + 1 is greater than the ‘ans’ value.

Step 5 − If the ‘prefSum’ value already exists in the map as a key, subtract its value from the current index, and if the resultant value is greater than the ‘ans’ value, update it.

Step 6 − Insert the current ‘prefSum’ into the map with the current index.

Step 7 − At the end, return the resultant value after subtracting the ‘ans’ from the binary string’s length.

Example

#include <bits/stdc++.h>
using namespace std;

int countRemoval(string bin_str) {
    // To store prefix sum index
    unordered_map<int, int> prefMap;
    int bin_len = bin_str.size();
    // To store prefix sum
    int prefSum = 0;
    // To store the length of the longest binary string having equal 0's and 1's
    int ans = 0;
    for (int p = 0; p < bin_len; p++) {
        // Update prefix sum according to '1' and '0'
        prefSum += ((bin_str[p] == '1') ? 1 : -1);
        // When binary strings have equal '0' and '1'.
        if (prefSum == 0) {
            ans = max(ans, p + 1);
        }
        // Find sum in the map
        if (prefMap.count(prefSum)) {
            // prefMap[prefSum] is the first index where the same prefix sum occurred. We can take the middle string as a valid string.
            ans = max(ans, p - prefMap[prefSum]);
        } else {
            // Update prefix sum
            prefMap[prefSum] = p;
        }
    }
    // Number of characters to remove from start and end
    return bin_len - ans;
}
int main() {
    string bin_str = "0011001";
    cout << "The number of removals required to make 1's and 0's equal is " << countRemoval(bin_str) << endl;
    return 0;
}

Output

The number of removals required to make 1's and 0's equal is 1

Time complexity − O(N) to find the largest string having an equal number of ‘0’ and ‘1’ counts.

Space complexity − O(N) to store prefix sum values in the map.

We used the same algorithm to find the largest substring with a sum equal to 0 to solve the problem. Programmers may use the brute force approach to solve the problem in which we can remove each character from the start or end to check whether the string contains the same number of ‘1’ and ‘0’.

Updated on: 17-Jul-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements