Count ways to partition a Binary String such that each substring contains exactly two 0s
Last Updated :
27 Apr, 2021
Given 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: 2
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 the 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++
#include <bits/stdc++.h>
using namespace std;
int totalWays( int n, string str)
{
vector< int > IdxOf0s;
int cntWays = 1;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' ) {
IdxOf0s.push_back(i);
}
}
int M = IdxOf0s.size();
if (M == 0 or M % 2) {
return 0;
}
for ( int i = 2; i < M; i += 2) {
cntWays = cntWays * (IdxOf0s[i]
- IdxOf0s[i - 1]);
}
return cntWays;
}
int main()
{
string str = "00100" ;
int n = str.length();
cout << totalWays(n, str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int totalWays( int n, String str)
{
ArrayList<Integer> IdxOf0s =
new ArrayList<Integer>();
int cntWays = 1 ;
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) == '0' )
{
IdxOf0s.add(i);
}
}
int M = IdxOf0s.size();
if ((M == 0 ) || ((M % 2 ) != 0 ))
{
return 0 ;
}
for ( int i = 2 ; i < M; i += 2 )
{
cntWays = cntWays * (IdxOf0s.get(i)
- IdxOf0s.get(i - 1 ));
}
return cntWays;
}
public static void main(String[] args)
{
String str = "00100" ;
int n = str.length();
System.out.print(totalWays(n, str));
}
}
|
Python3
def totalWays(n, str ):
IdxOf0s = []
cntWays = 1
for i in range (n):
if ( str [i] = = '0' ):
IdxOf0s.append(i)
M = len (IdxOf0s)
if (M = = 0 or M % 2 ):
return 0
for i in range ( 2 , M, 2 ):
cntWays = cntWays * (IdxOf0s[i] -
IdxOf0s[i - 1 ])
return cntWays
if __name__ = = '__main__' :
str = "00100"
n = len ( str )
print (totalWays(n, str ))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static int totalWays( int n, string str)
{
ArrayList IdxOf0s
= new ArrayList();
int cntWays = 1;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
{
IdxOf0s.Add(i);
}
}
int M = IdxOf0s.Count;
if ((M == 0) || ((M % 2) != 0)) {
return 0;
}
for ( int i = 2; i < M; i += 2)
{
cntWays = cntWays * (Convert.ToInt32(IdxOf0s[i]) -
Convert.ToInt32(IdxOf0s[i - 1]));
}
return cntWays;
}
static public void Main()
{
string str = "00100" ;
int n = str.Length;
Console.Write(totalWays(n, str));
}
}
|
Javascript
<script>
function totalWays(n, str)
{
var IdxOf0s = [];
var cntWays = 1;
for ( var i = 0; i < n; i++) {
if (str[i] == '0' ) {
IdxOf0s.push(i);
}
}
var M = IdxOf0s.length;
if (M == 0 || M % 2) {
return 0;
}
for ( var i = 2; i < M; i += 2) {
cntWays = cntWays * (IdxOf0s[i]
- IdxOf0s[i - 1]);
}
return cntWays;
}
var str = "00100" ;
var n = str.length;
document.write( totalWays(n, str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...