Count binary Strings that does not contain given String as Subsequence

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;

}

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 2ntimes. 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;

}

Time Complexity: O(N)
Auxiliary Space: O(N)

Related Articles:

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: