The Wayback Machine - https://web.archive.org/web/20210111152854/https://www.geeksforgeeks.org/count-ways-to-partition-a-binary-string-such-that-each-substring-contains-exactly-two-0s/
Related Articles

Related Articles

Count ways to partition a Binary String such that each substring contains exactly two 0s
  • Last Updated : 11 Jan, 2021

Given a binary string str, the task is to find the count of ways to partition the string such that each partitioned substring contains exactly two 0s.

Examples:

Input: str = “00100”
Output:
Explanation:
Possible ways to partition the string such that each partition contains exactly two 0s are: { {“00”, “100”}, {“001”, “00”} }.
Therefore, the required output is 2.

Input: str = “000”
Output: 0

Approach: The idea is to calculate the count of 1s between every two consecutive 0s of the given string. Follow the steps below to solve the problem:



  • Initialize an array, say IdxOf0s[], to store the indices of 0s in the given string.
  • Iterate over the characters of the given string and store the indices of the 0s into IdxOf0s[].
  • Initialize a variable, say cntWays, to store the count of ways to partition the string such that each partition contains exactly two 0s.
  • If count of 0s in the given string is odd, then update cntWays = 0.
  • Otherwise, traverse the array IdxOf0s[] and calculate the count of ways to partition the array having each partition exactly two 0s using cntWays *= (IdxOf0s[i] – IdxOf0s[i – 1])
  • Finally, print the value of cntWays.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find count of ways to partition
// the string such that each partition
// contains exactly two 0s.
int totalWays(int n, string str)
{
  
    // Stores indices of 0s in
    // the given string.
    vector<int> IdxOf0s;
  
    // Store the count of ways to partition
    // the string such that each partition
    // contains exactly two 0s.
    int cntWays = 1;
  
    // Iterate over each characters
    // of the given string
    for (int i = 0; i < n; i++) {
  
        // If current character is '0'
        if (str[i] == '0') {
  
            // Insert index
            IdxOf0s.push_back(i);
        }
    }
  
    // Stores total count of 0s in str
    int M = IdxOf0s.size();
  
    if (M == 0 or M % 2) {
  
        return 0;
    }
  
    // Traverse the array, IdxOf0s[]
    for (int i = 2; i < M; i += 2) {
  
        // Update cntWays
        cntWays = cntWays * (IdxOf0s[i]
                             - IdxOf0s[i - 1]);
    }
  
    return cntWays;
}
  
// Driver Code
int main()
{
    string str = "00100";
  
    int n = str.length();
  
    cout << totalWays(n, str);
  
    return 0;
}

chevron_right


Output:

2

Time Complexity: O(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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :