Count binary Strings that does not contain given String as Subsequence
Last Updated :
30 Mar, 2023
Given a number N and string S, count the number of ways to create a binary string (containing only ‘0’ and ‘1’) of size N such that it does not contain S as a subsequence.
Examples:
Input: N = 3, S = “10”.
Output: 4
Explanation: There are 8 strings possible to fill 3 positions with 0’s or 1’s. {“000”, “001”, “010”, “100”, “”011”, “110”, “101”, “111”} and only {“000”, “001”, “011”, “111”} are valid strings that do not contain “10” as a subsequence. so the answer will be 4.
Input: N = 5, S = “1010”
Output: 26
Naive approach: The basic way to solve the problem is as follows:
The first thing to be observed is that answer does not depends upon the string S itself but only depends upon the size of S. There are N positions to fill of binary string with either 1 or 0 avoiding S forming its subsequence. There is a need to keep track of matches as well which stores how many characters of a given string S have been matched with a string that is being created. if it is equal to the size of S then return 0 since the strings will be invalid from there on. the recursive function will be called for each position ‘i’ as Match and No Match.
Follow the steps below to solve the problem:
- Create a recursive function that takes two parameters one is the position that needs to fill and the other is how many of the characters have been matched from string S.
- Check if all characters are matched then return 0.
- Check the base cases. If the value of i is equal to N return 1.
- Call the recursive function for both Match and NoMatch and Sum up the values that are returned.
- return the value sum.
Below is the code to implement the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0;
if (i == N)
return 1;
int ans = recur(i + 1, curMatch + 1, S, N)
+ recur(i + 1, curMatch, S, N);
return ans;
}
void countBinStrings( int N, string S)
{
int sizeOfString = S.size();
cout << recur(0, 0, sizeOfString, N) << endl;
}
int main()
{
int N = 3;
string S = "10" ;
countBinStrings(N, S);
int N1 = 5;
string S1 = "1010" ;
countBinStrings(N1, S1);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0 ;
if (i == N)
return 1 ;
int ans = recur(i + 1 , curMatch + 1 , S, N)
+ recur(i + 1 , curMatch, S, N);
return ans;
}
static void countBinStrings( int N, String S)
{
int sizeOfString = S.length();
System.out.println(recur( 0 , 0 , sizeOfString, N));
}
public static void main (String[] args)
{
int N = 3 ;
String S = "10" ;
countBinStrings(N, S);
int N1 = 5 ;
String S1 = "1010" ;
countBinStrings(N1, S1);
}
}
|
Python3
def count_bin_strings(N, S):
size_of_string = len (S)
def recur(i, cur_match):
if cur_match = = size_of_string:
return 0
if i = = N:
return 1
ans = recur(i + 1 , cur_match + 1 ) + recur(i + 1 , cur_match)
return ans
print (recur( 0 , 0 ))
if __name__ = = '__main__' :
N = 3
S = "10"
count_bin_strings(N, S)
N1 = 5
S1 = "1010"
count_bin_strings(N1, S1)
|
C#
using System;
public class GFG {
static int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0;
if (i == N)
return 1;
int ans = recur(i + 1, curMatch + 1, S, N)
+ recur(i + 1, curMatch, S, N);
return ans;
}
static void countBinStrings( int N, string S)
{
int sizeOfString = S.Length;
Console.WriteLine(recur(0, 0, sizeOfString, N));
}
public static void Main( string [] args)
{
int N = 3;
string S = "10" ;
countBinStrings(N, S);
int N1 = 5;
string S1 = "1010" ;
countBinStrings(N1, S1);
}
}
|
Javascript
function recur(i, curMatch, S, N)
{
if (curMatch == S)
return 0;
if (i == N)
return 1;
let ans = recur(i + 1, curMatch + 1, S, N)
+ recur(i + 1, curMatch, S, N);
return ans;
}
function countBinStrings(N, S)
{
let sizeOfString = S.length;
document.write(recur(0, 0, sizeOfString, N));
}
let N = 3;
let S = "10" ;
countBinStrings(N, S);
document.write( "<br>" );
let N1 = 5;
let S1 = "1010" ;
countBinStrings(N1, S1);
|
Time Complexity: O(2n)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following idea:
The idea is similar, but it can be observed that there are N * 5 states but the recursive function is called 2n times. That means that some states are called repeatedly. So the idea is to store the value of states. This can be done using recursive structure intact and just store the value in an array or HashMap and whenever the function is called, return the value store without computing .
dp[i][j] = X represents count of binary strings of size i and j matches has done from string S.
Follow the steps below to solve the problem:
- Create a 2d array of dp[N + 1][5] initially filled with -1.
- If the answer for a particular state is computed then save it in dp[i][curMatch].
- If the answer for a particular state is already computed then just return dp[i][curMatch].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int dp[100001][5];
int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0;
if (i == N)
return 1;
if (dp[i][curMatch] != -1)
return dp[i][curMatch];
int ans = recur(i + 1, curMatch + 1, S, N)
+ recur(i + 1, curMatch, S, N);
return dp[i][curMatch] = ans;
}
void countBinStrings( int N, string S)
{
memset (dp, -1, sizeof (dp));
int sizeOfString = S.size();
cout << recur(0, 0, sizeOfString, N) << endl;
}
int main()
{
int N = 3;
string S = "10" ;
countBinStrings(N, S);
int N1 = 5;
string S1 = "1010" ;
countBinStrings(N1, S1);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int [][] dp = new int [ 100001 ][ 5 ];
static int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0 ;
if (i == N)
return 1 ;
if (dp[i][curMatch] != - 1 )
return dp[i][curMatch];
int ans = recur(i + 1 , curMatch + 1 , S, N)
+ recur(i + 1 , curMatch, S, N);
return dp[i][curMatch] = ans;
}
static void countBinStrings( int N, String S)
{
for ( int [] row : dp)
Arrays.fill(row, - 1 );
int sizeOfString = S.length();
System.out.println(recur( 0 , 0 , sizeOfString, N));
}
public static void main(String[] args)
{
int N = 3 ;
String S = "10" ;
countBinStrings(N, S);
int N1 = 5 ;
String S1 = "1010" ;
countBinStrings(N1, S1);
}
}
|
Python3
dp = [[ - 1 ] * 5 for _ in range ( 100001 )];
def recur(i, curMatch, S, N):
if (curMatch = = S):
return 0 ;
if (i = = N):
return 1 ;
if (dp[i][curMatch] ! = - 1 ):
return dp[i][curMatch];
ans = recur(i + 1 , curMatch + 1 , S, N) + recur(i + 1 , curMatch, S, N);
dp[i][curMatch] = ans;
return dp[i][curMatch];
def countBinStrings( N, S):
for i in range ( 0 , 100001 ):
for j in range ( 0 , 5 ):
dp[i][j] = - 1 ;
sizeOfString = len (S);
print (recur( 0 , 0 , sizeOfString, N));
N = 3 ;
S = "10" ;
countBinStrings(N, S);
N1 = 5 ;
S1 = "1010" ;
countBinStrings(N1, S1);
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static int [,] dp= new int [100001, 5];
static int recur( int i, int curMatch, int S, int N)
{
if (curMatch == S)
return 0;
if (i == N)
return 1;
if (dp[i, curMatch] != -1)
return dp[i,curMatch];
int ans = recur(i + 1, curMatch + 1, S, N)
+ recur(i + 1, curMatch, S, N);
return dp[i,curMatch] = ans;
}
static void countBinStrings( int N, string S)
{
for ( int i=0; i<100001; i++)
for ( int j=0; j<5; j++)
dp[i,j]=-1;
int sizeOfString = S.Length;
Console.WriteLine(recur(0, 0, sizeOfString, N));
}
public static void Main ( string [] args)
{
int N = 3;
string S = "10" ;
countBinStrings(N, S);
int N1 = 5;
string S1 = "1010" ;
countBinStrings(N1, S1);
}
}
|
Javascript
let dp = [];
for (let i = 0; i<100001; i++){
dp[i] = new Array(5);
}
function recur(i, curMatch, S, N){
if (curMatch == S) return 0;
if (i == N) return 1;
if (dp[i][curMatch] != -1)
return dp[i][curMatch];
let ans = recur(i+1, curMatch+1, S, N) + recur(i+1, curMatch, S, N);
return dp[i][curMatch] = ans;
}
function countBinStrings(N, S){
for (let i = 0; i<100001; i++){
for (let j = 0; j<5; j++){
dp[i][j] = -1;
}
}
let sizeOfString = S.length;
console.log(recur(0,0,sizeOfString, N));
}
let N = 3;
let S = "10" ;
countBinStrings(N, S);
let N1 = 5;
let S1 = "1010" ;
countBinStrings(N1, S1);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach : Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memorization(top-down) because memorization method needs extra stack space of recursion calls.
Steps to solve this problem :
- Create a table to store the solution of the subproblems.
- Initialize the table with base cases
- Fill up the table iteratively
- Return the final solution
Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
int countBinStrings( int N, int S)
{
int dp[N+1][S+1];
for ( int i=0; i<=N; i++){
for ( int j=0; j<=S; j++){
if (j==S){
dp[i][j] =0;
}
else if (i==N){
dp[i][j] = 1;
}
}
}
for ( int i=N-1; i>=0; i--){
for ( int j=S-1; j>=0; j--){
dp[i][j]= dp[i+1][j+1] + dp[i+1][j];
}
}
return dp[0][0];
}
int main()
{
int N = 3;
string S = "10" ;
cout << countBinStrings(N, S.size()) << endl;
int N1 = 5;
string S1 = "1010" ;
int s = S1.size();
cout << countBinStrings(N1, s) << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
static int countBinStrings( int N, int S)
{
int dp[][] = new int [N + 1 ][S + 1 ];
for ( int i = 0 ; i <= N; i++) {
for ( int j = 0 ; j <= S; j++) {
if (j == S) {
dp[i][j] = 0 ;
}
else if (i == N) {
dp[i][j] = 1 ;
}
}
}
for ( int i = N - 1 ; i >= 0 ; i--) {
for ( int j = S - 1 ; j >= 0 ; j--) {
dp[i][j] = dp[i + 1 ][j + 1 ] + dp[i + 1 ][j];
}
}
return dp[ 0 ][ 0 ];
}
public static void main(String[] args)
{
int N = 3 ;
String S = "10" ;
System.out.println(countBinStrings(N, S.length()));
int N1 = 5 ;
String S1 = "1010" ;
int s = S1.length();
System.out.println(countBinStrings(N1, s));
}
}
|
Python3
def countBinStrings(N, S):
dp = [[ 0 for j in range (S + 1 )] for i in range (N + 1 )]
for i in range (N + 1 ):
for j in range (S + 1 ):
if j = = S:
dp[i][j] = 0
elif i = = N:
dp[i][j] = 1
for i in range (N - 1 , - 1 , - 1 ):
for j in range (S - 1 , - 1 , - 1 ):
dp[i][j] = dp[i + 1 ][j + 1 ] + dp[i + 1 ][j]
return dp[ 0 ][ 0 ]
if __name__ = = "__main__" :
N = 3
S = "10"
print (countBinStrings(N, len (S)))
N1 = 5
S1 = "1010"
s = len (S1)
print (countBinStrings(N1, s))
|
C#
using System;
public class Program {
public static int CountBinStrings( int N, int S)
{
int [, ] dp = new int [N + 1, S + 1];
for ( int i = 0; i <= N; i++) {
for ( int j = 0; j <= S; j++) {
if (j == S) {
dp[i, j] = 0;
}
else if (i == N) {
dp[i, j] = 1;
}
}
}
for ( int i = N - 1; i >= 0; i--) {
for ( int j = S - 1; j >= 0; j--) {
dp[i, j] = dp[i + 1, j + 1] + dp[i + 1, j];
}
}
return dp[0, 0];
}
public static void Main()
{
int N = 3;
string S = "10" ;
Console.WriteLine(CountBinStrings(N, S.Length));
int N1 = 5;
string S1 = "1010" ;
int s = S1.Length;
Console.WriteLine(CountBinStrings(N1, s));
}
}
|
Javascript
function countBinStrings(N, S) {
let dp = new Array(N + 1);
for (let i = 0; i <= N; i++) {
dp[i] = new Array(S + 1);
for (let j = 0; j <= S; j++) {
if (j == S) {
dp[i][j] = 0;
} else if (i == N) {
dp[i][j] = 1;
}
}
}
for (let i = N - 1; i >= 0; i--) {
for (let j = S - 1; j >= 0; j--) {
dp[i][j] = dp[i + 1][j + 1] + dp[i + 1][j];
}
}
return dp[0][0];
}
let N = 3;
let S = "10" ;
console.log(countBinStrings(N, S.length));
let N1 = 5;
let S1 = "1010" ;
let s = S1.length;
console.log(countBinStrings(N1, s));
|
Time Complexity: O(N * S), where S is the length of the string and N is mentioned in the problem statement
Auxiliary Space: O(N * S), where S is the length of the string and N is mentioned in the problem statement
Related Articles: