Given a binary string S of size N, the task is to find the maximum number of partitions of string S such that the ratio of the number of 0 and 1 in all the partitions are the same.
Examples:
Input: S = “010100001”
Output: 3
Explanation:
The substring [0, 2], [3, 5] and [6, 8] have the same ratio of ‘0′ and ‘1’ is 2:1. Therefore, the maximum possible partition is 3.Input: str=”001101″
Output: 2
Naive Approach: The naive approach is to generate all the partitions of string S and find the partition that has a maximum frequency of 0 to 1 ratio.
Time Complexity: O(N*2N)
Auxiliary Space: O(N)
Efficient Approach: To solve this problem first see some observations:
Let say there are two prefix strings that obtain the same ratio of 0 and 1. Then it is possible to divide the larger prefix into two with the same ratio. For eg- S = “0101” the prefix [0, 1] has the ratio of 1:1 and the prefix[0, 3] has the ratio of 1:1 then it is possible to partition the string in two with the ratio 1:1. Therefore, just count the number of prefixes that has the same 0 and 1 ratio as that of string S and print the number of prefix with that ratio.
Follow the steps below to solve the problem:
- Initialize two integer variables, say count_of_0 and count_of_1 as 0 to store the count of the number of ‘0’ and ‘1’ characters respectively.
- Initialize a hash map, say M which stores the frequency of ratio of ‘0′ to ‘1′.
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- If S[i] is equal to ‘0’ then increment the count_of_0 by 1, Otherwise increment count_of_1 by 1.
- Find the GCD of count_of_0 and count_of_1 and store it in a variable, say GCD.
- If GCD = 0, then increment m[{count_of_0, count_of_1}] by 1.
- If GCD != 0, then increment the value of m[{count_of_0 / GCD, count_of_1 / GCD}] by 1.
- Print the value of m[{count_of_0 / GCD, count_of_1 / GCD}] as the answer.
Below is the implementation of the above approach :
C++
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find maximum partition such// that the ratio of 0 and 1 are samevoid maximumPartition(string S){ // Size of string int N = S.size(); // Variable to store the frequency // of 0 and 1 int count_of_0 = 0, count_of_1 = 0; // Map to store frequency of ratio map<pair<int, int>, int> m; int ans; // Traverse the string for (int i = 0; i < N; i++) { // Increment the frequency // of 0 by 1 if s[i] = 0 if (S[i] == '0') count_of_0++; // Otherwise increment frequency of 1 else count_of_1++; int first = count_of_0, second = count_of_1; // Find GCD of count_of_0 and count_of_1 int GCD = __gcd(count_of_0, count_of_1); // Convert the count of 0 and count of 1 // in the coprime numbers if (GCD != 0) { first = count_of_0 / GCD; second = count_of_1 / GCD; } // Increase the ratio of 0 and 1 by 1 m[{ first, second }]++; ans = m[{ first, second }]; } cout << ans << endl;} // Driver Codeint main(){ // Given Input string S = "001101"; // Function Call maximumPartition(S); return 0;} |
2
Time complexity: O(N log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


