Maximum count of unique index 10 or 01 substrings in given Binary string


In this problem, we will count the maximum number of 10 and 01 pairs that can be formed using the given binary string.

To solve the problem, we can check the number of 10 and 01 pairs we can form using adjacent characters without sharing any characters in two pairs.

Problem Statement

We have given a bin_str binary string. We need to count the maximum number of 10 and 01 pairs we can make using the adjacent characters only. Also, we can use one character in any single pair. Two pairs can't share a single character.

Sample Examples

Input –  bin_str = "1100101"
Output – 3

Explanation

We can make a '10' pair at the 1st (0-based indexing) index and 01 pair at the 3rd and 5th index.

Input –  bin_str = "111111";
Output – 0

Explanation

The string contains all '1'. So, we can't make any 10 or 01 pairs.

Input –  bin_str = "101";
Output – 1

Explanation

We can't share a single character in two pairs. So, we can either make only 10 or 01 pair.

Approach 1

In this approach, we will use the boolean variable to track whether the previous character is used with the previous pair. So, we can make the unique 10 and 01 pairs without sharing adjacent characters.

Also, we will count the number of different adjacent characters to get the count of 01 and 10 pairs.

Algorithm

  • Step 1 − Initialize the 'cnt' with 0 to store the count of pairs and 'isPrev' with true to track whether the previous character is available to use to make a pair.

  • Step 2 − Start traversing the binary string from the 1st index.

  • Step 3 − If characters at p and p – 1 index are different and 'isPrev' true, increment the 'cnt' value by 1 as we find the pair of 01 or 10. Also, update the 'isPrev' with false as the current character is not available to make a pair with the next character.

  • Step 4 − Else, update the 'isPrev' with true.

  • Step 5 − At last, return the 'cnt' value.

Example

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

int countPairs(string bin_str) {

   // To store the count of pairs
   int cnt = 0;

   // To track whether we can use the previous character in the pair
   bool isPrev = true;

   // Iterate the string
   for (int p = 1; p < bin_str.size(); p++) {

      // Check whether adjacent characters are different and previous characters available to make a pair
      if (bin_str[p] != bin_str[p - 1] && isPrev) {

         // Current character gets used
         isPrev = false;
         cnt++;
      } else {

         // Make the current character free for the next pair
         isPrev = true;
      }
   }
   return cnt;
}
int main() {
   string bin_str = "1100101";
   cout << "The maximum number of 01 and 10 cnt is " << countPairs(bin_str);
   return 0;
}

Output

The maximum number of 01 and 10 cnt is 3
  • Time Complexity − O(N) for traversing the string.

  • Space Complexity − O(1)

We have counted the number of 10 and 01 pairs we can make using the adjacent characters of the binary string. Programmers may count the maximum number of 100 and 001 pairs we can form in the given binary string. In this case, we need to use the integer variable to check whether the previous two characters are available to use rather than using the single Boolean value.

Updated on: 05-Oct-2023

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements