Minimize Replacement of Bits to make the Count of 01 Substring Equal to 10 Substring


Problem statement − We have given the binary string of length N. We need to find the minimum number of flipping characters required to get the balanced binary string. Flipping characters means converting 0 to 1 and 1 to 0. If any string contains an equal number of ‘01’ and ‘10’ pairs, we can say that string is a balanced binary string.

Sample examples

Input

str = "001010"

Output

0

Explanation − The string contains 2 ‘01’ and ‘10’ pairs. So, we don’t need to perform any flipping operations.

Input

 str = ‘00001’

Output

1

Explanation − The string contains 1 ‘01’ pair but doesn’t contains a ‘10’ pair, so we need to make 1 flipping operation.

Input

 str = ‘1’

Output

0

Explanation − The input string is already balanced.

Observation − We can notice that binary string always contains the same numbers of 01 and 10 pairs if the first and last character of the string is equal.

Let’s look at the below examples.

  • 1 − The string is balanced, as the first and last character is the same.

  • 0 − balanced string.

  • 10 − The first and last characters are not the same, so the string is unbalanced.

  • 101 − balanced string.

  • 010 − balanced string.

  • 1111 − balanced string.

  • 01010 − balanced string.

  • 0101 − unbalanced string.

Approach 1

In this approach, we will use the map data structure to store the total number of ‘01’ and ‘10’ pairs of the given string. After that, we can take the difference between the number of pair to find a minimum number of flipping.

Algorithm

Step 1 − Define the ‘count’ map to store the pairs of the string and int.

Step 2 − Also, define the ‘temp’ string to store the temporary string.

Step 3 − Traverse the string till the last second index.

Step 4 − In the temp string, store the substring of length 2 starting from the pth index.

Step 5 − Increase the ‘temp’ string count in the map.

Step 6 − Take the absolute difference between the count[01] and count[10] and return its value.

Example

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

int totalReplacement(string alpha) {
    unordered_map<string, int> count;
    string temp;
    // count the number of occurrences of 01 and 10
    for (int p = 0; p < alpha.length() - 1; p++) {
        // substring of length 2 starting from index p
        temp = alpha.substr(p, 2);
        // Increase the count of temp by 1
        count[temp]++;
    }
    // return the absolute difference between the count of 01 and 10
    return abs(count["10"] - count["01"]);
}
int main() {
    string str = "001010";
    cout << "The total number of replacements required to make tota 01 equal to 10 is: " << totalReplacement(str) << endl;
    return 0;
}

Output

The total number of replacements required to make tota 01 equal to 10 is: 0

Time complexity− O(N) as we iterate the string.

Space complexity − O(2) ~ O(1) as we use the map.

Approach 2

In this approach, we will use count variables to store the count of 01 and 10 pairs of strings rather than using the map. It improves the space complexity of the code.

Algorithm

Step 1 − Define the ‘temp’ string, and initialize the ‘count01’ and ‘count10’ variables with zero.

Step 2 − Use the for loop to traverse the string.

Step 3 − Take the substring of length 2.

Step 4 − If the temp is equal to the ‘01’, increase the value of ‘count01’ by 1. Otherwise, increase the value of the ‘count10’ by 1.

Step 5 − Return the absolute difference between count01 and count10.

Example

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

int totalReplacement(string alpha) {
    string temp;
    int cnt01 = 0, cnt10 = 0;
    // count the number of occurrences of 01 and 10
    for (int p = 0; p < alpha.length() - 1; p++) {
        // substring of length 2 starting from index p
        temp = alpha.substr(p, 2);
        if (temp == "01") {
            cnt01++;
        } else {
            cnt10++;
        }
    }
    // return the absolute difference between the count of 01 and 10
    return abs(cnt01 - cnt10);
}
int main() {
    string str = "010101010101";
    cout << "The total number of replacements required to make tota 01 equal to 10 is: " << totalReplacement(str) << endl;
    return 0;
}

Output

The total number of replacements required to make tota 01 equal to 10 is: 1

Time complexity− O(N) as we iterate the string.

Space complexity − O(1) as we use the constant space.

We will get either 0 or 1 in the output for any given binary string, as we need to make the first and last characters same according to the count of 01 and 10 pairs.

Updated on: 14-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements