Count of possible distinct Binary strings after replacing 11 with 0


A binary string is a string that contains only two types of different characters zeroes and ones. We can replace a substring '11' of the given string with another string '0' and we have to find the number of different possible strings we can get from it. We are going to use dynamic programming to get the solution as other methods may take exponential time complexity.

Sample Example

Input

string str = 11010

Output

2

Explanation

We can replace the first two numbers with the zero and can get another string 0010 and the second string is the given string.

Brute Force

The brute force approach is not going to perform better here, as we are getting the exponential time complexity. We need to change every substring of '11' to '0' one by one and then we are going to find the exact count.

To improve the time complexity of the brute force method we are going to use the memoization technique which is nothing but the dynamic programming approach in which we are going to store each case that is already calculated.

Approach 1: Dynamic Programming

In this approach, we are going to traverse over the string and store the number of cases that can be formed for the current index and then getting the final result by traversing over that array.

Example

#include <bits/stdc++.h>
using namespace std; 

// function to get the final result 
int getCount(string str){
   int len = str.length(); // getting lenght of the string 
   vector<int>dp(len,0); // vector to store result  
   // pre-defining edge cases
   if(str[0] == '1'){
      dp[0] = 1;
   }
   if(str.substr(0,2) == "11") {
      dp[1] = 2;
   } else if (str[1] == '1') {
      dp[1] = 1;
   }
   // traversing over the string & storing the value of each step
   for (int i = 2; i < len; i++){
      if(str[i] == '1'){
         // checking for the previous index if it is also one 
         if(str[i-1] == '1'){
            // checking if the previous 2 indexes are '1' then the current spot will have two cases
            if(str[i - 2] == '1'){
               dp[i] = dp[i-1] + dp[i-2];
            } else {
               dp[i] = 2;
            }
         } else {
            // if current support is zero then there will will only one case 
            dp[i] = 1;
         }
      }
   }
   int res = 1; 
   // getting the result by multiplying the stored data with the result 
   for(int i = 1; i < len; ++i){
      if(dp[i] < dp[i-1]){
         res = res * dp[i-1];
      }
   }
    
   // storing the last bit
   if(dp[len-1] != 0){
      res = res * dp[len - 1];
   }

   return res; // returning the final result 
}

int main(){
   string str = "11011"; // given string
   // calling the given function 
   cout<<"The count of possible distinct Binary strings after replacing 11 with 0 is: " << getCount(str)<<endl;
   return 0;
}

Output

The count of possible distinct Binary strings after replacing 11 with 0 is: 4

Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the given string. We are getting linear time complexity because we are traversing over the array only once.

The space complexity of the above code is O(N), as we are using an extra array to store the results.

Approach 2: Memory Efficient Dynamic Programming

In the previous code, we have stored each index value, but we can get the answer by just storing the last three.

So, in this approach we will make the space complexity constant.

Example

#include <bits/stdc++.h>
using namespace std; 

// function to get the final result 
int getCount(string str){
   int len = str.length(); // getting lenght of the string     
   int first = 0, second = 0, third = 0; 
   // pre-defining edge cases
   if(str[0] == '1'){
      first = 1;
   }
   if(str.substr(0,2) == "11") {
      second = 2;
   } else if (str[1] == '1') {
      second = 1;
   }
   // traversing over the string & storing the value of each step
   for (int i = 2; i < len; i++){
      if(str[i] == '1'){
         // checking for the previous index if it is also one 
         if(str[i-1] == '1'){
            // checking if the previous 2 indexes are '1' then the current spot will have two cases
            if(str[i - 2] == '1'){
               int cur = third;
               third = first + second;
               first = second;
               second = cur;
            } else {
               third = second;
               second = 2;
               first = second;
            }
         } else {
            // if current support is zero then there will will only one case 
            third = second;
            second = 1; 
            first = second;
         }
      } else {
         third = second;
         second = 0;
         first = second;
      }
   } 
   int res = 1; 
   // getting the result by multiplying the stored data with the result 
   if(second > third){
      res = res * second;
   } else {
      res = res * third;
   }    
   // storing the last bit
   if(first != 0){
      res = res * first;
   }
   return res; // returning the final result 
} 
int main(){
   string str = "11011"; // given string 
   // calling the given function 
   cout<<"The count of possible distinct Binary strings after replacing 11 with 0 is: " << getCount(str)<<endl;
   return 0;
}

Output

The count of possible distinct Binary strings after replacing 11 with 0 is: 4

Conclusion

In this tutorial, we have implemented a program to find the count of different numbers of strings that we can get from the given binary string if we can replace any '11' with '0'. We have explained the brute force method which was in-efficient and we have later implemented a dynamic programming approach in two ways with linear time complexity and linear and constant space complexity.

Updated on: 24-Aug-2023

37 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements