Maximize partitions in a given Binary String having the same ratio of 0s and 1s


A binary string is a string that contains only zeros and ones as the different types of characters. We have given a binary string and the task is to divide it into some number of partitions (possibly zero) where each partition contains an equal ratio of zero and one. We will use the hashmap to solve the problem with efficient time and space complexity.

Sample Examples

Input 1:

string str = 100010001

Output: 3

Explanation The given string can be partitioned into three substrings that will contain the same ratio of zeros and ones. We can break the string into ranges [0,2], [3,5], and [6,8] all of them have a ratio of zeros and ones is 2:1.

Input 2

string str = 01000111

Output: 2

Explanation The given string can be divided into two substrings first, in the range [0,1], and second at the range [2,7]. Both regions will contain the same ratio of 1:1.

Naive Approach

In this approach, we can find all the ways to partition the substring and then will check for them which contains the same ratio of the zeros and ones.

The problem with this approach is the time complexity, to generate all the partitions of the string will cost 2^N steps hence the O(2^N) time complexity for the partitions only then to check the ratio a factor of N will be there.

Also, to generate all the partitions we use recursion that will cost O(N) space complexity which means the current approach is not efficient.

HashMap Approach

In this approach, we will use the hashmap to reduce the time complexity. Let us first see the basic idea −

The basic idea behind this approach is if any prefix has the same ratio of zeros and ones, then we move further in the string and again got the same ratio at any index then we can make a partition there. As we are considering the whole string to the ratio for the complete string will be a prefix because we have to end there.

Steps

First, we will create a hash map with the key as the pair of integers and the value will be an integer. Then we will move over the string and maintain two variables to count the numbers of zeros and ones.

We will find the GCD of the numbers and will divide them with it to store the shortest form of both the numbers representing the ratio.

Will store the ratio in the form of pair and will increase the count of the current pair.

We will maintain an integer to store the answer and will update it will each iteration and for the last iteration or index it will be the final answer.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the solution 
int maxPart(string str){
   int len = str.length(); // variable to find the length of the string 
   int ans = 0; // variable to store the answer
   map<pair<int,int>,int>mp; // defining hashmap    
   // variable to maintain the count of zeros and ones 
   int zcount = 0, ocount = 0;    
   // traversing over the string 
   for(int i=0; i<len; i++){
      if(str[i] == '0'){
         zcount++;
      }
      else{
         ocount++;
      }        
      // getting gcd
      int gcd = __gcd(zcount,ocount);        
      // dividing by gcd to get the ratio 
      int first = zcount/gcd;
      int second = ocount/gcd;        
      // adding values to hashmap 
      mp[{first,second}]++;
      ans = mp[{first,second}];
   }
   return ans;
}
int main(){
   string str = "100010001"; // given string     
   // calling the function to get the maximum number of partitions for the ratio
   cout<<"The maximize partitions in the given Binary String having the same ratio of 0s and 1s is "<<maxPart(str)<<endl;
   return 0;
}

Output

The maximize partitions in the given Binary String having the same ratio of 0s and 1s is 3

Time and Space Complexity

The time complexity of the above code is O(N*log(N)), where N is the size of the given array. We are storing the elements in the hashmap cost us the extra logarithmic time factor.

The space complexity of the above code is O(N), which is due to the hashmap as there could be N different pairs possible.

Note We have seen in the hashmap approach that the last ratio will be answer of the problem. This leads to a new approach which is more efficient as compared to the hashmap in terms of both time and space complexity.

We can traverse over the string and find the total count of zeros and ones and then get their count and ratio.

Again, we will traverse over the string and will count the zeros and ones. At each index, we will check the ratio, if the ratio is equal to the final ratio, then we can mark it as a partition.

This will give us the best technique with the linear time complexity for searching but extra logarithmic factor for the GCD and constant space complexity.

Conclusion

In this tutorial, we have implemented three approached to find the maximum number of partitions that can be done by on the basis of same ratio of number of zeros to number of ones present in the particular substring. We have discussed three approaches, one with the finding all the possible partitions which is the worst method and another is by using the hashmap find the total ratio and count partition when it occurs.

Updated on: 17-May-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements