Find Binary string by converting all 01 or 10 to 11 after M iterations


A binary string is a string that consists of only two different types of characters and that is ‘0’ and ‘1’. We will be given a binary string and the number m. We have to apply the operation to convert all the consecutive occurrences of the 0 and 1 that are ‘01’ and ‘10’ to ‘11’. One more condition is there that there must be only one neighbor of ‘0’ can be ‘1’. We can traverse over the string only m times where m will be given.

Let’s understand with the following example

Input 1:
Given binary string: ‘01000101’
Given number: 2
Output: 111011101

Explanation

We will first traverse over the string and can change the zeros which have exactly one ‘1’ as the neighbour, which makes our string ‘111011101’. In the second iteration, the string will remain the same as there is no zero present which has exactly a single one present as the neighbour.

Input 2:
Given binary string: ‘00100’
Given number: 1000
Output: 11111 

Explanation

After the first iteration we will get 01110 and after the second iteration we will get 11111. For the remaining iterations, there will be no change in the string.

Approach

We have seen the example above for the given string, let us move to the approach −

  • First, we will create a function that will take the given string and the number as the parameter and will return the required string as the return value.

  • In the function, we will find the minimum among the length of the string and the given number of iterations as the string will maximum change in the given number of iterations.

  • Then we will iterate over the string given a number of iterations times and in each iteration will find the zero.

  • If the zero is between two ones or zeros then we will move otherwise we will update the zero to the one and move to the position.

  • From the main function, we will call the function and after calling it we will print the final answer.

Example

#include <iostream>
using namespace std;
string updateString(string str, int m){
   // using the optimal concept to reduce the number of iterations 
   int n = str.size(); // getting the size of the string 
   m = min(m,n); // getting the minimum value 
   // iterating using a while loop 
   while(m--){
      // iterating over the string 
      for(int i=0; i<n; i++){
         if(str[i] == '0'){
            if(i == 0){
               if(str[i+1] == '1'){
                  str[i] = '1';
               }
            }
            else if(i == n-1){
               if(str[i-1] == '1'){
                  str[i] = '1';
               }
            }
            else{
               if((str[i-1] == '1' || str[i+1] == '1') && (str[i-1] != str[i+1])){
                  str[i] = '1';
               }
            }
         }
      }
   }
   // returning the string 
   return str;
}
// main function 
int main(){
   string str = "01000101";// given string 
   int m = 2; // given number
   // calling the function to update the string 
   string update_str = updateString(str, m);
   cout<<"The given string "<< str << " after " << m << " number of iterations is "<< update_str<<endl;
   return 0;
}

Output

The given string 01000101 after 2 number of iterations is 11110101

Time and Space Complexity

The time complexity of the above code is O(min(M, N)*N), where N is the size of the string and M is the total no of iterations that we performed over the string.

The space complexity of the above code is O(N), as we are passing the string as the parameter to the given function.

Using Flag Method

In the previous method, we are iterating over the string total of the minimum of M or the length of the string. We can reduce the time complexity by using the flag to mark that the string is not changing.

Example

#include <iostream>
using namespace std;
string updateString(string str, int m){
   // using the optimal concept to reduce the number of iterations 
   int n = str.size(); // getting the size of the string 
   m = min(m,n); // getting the minimum value 
   bool flag = true;
   // iterating using while loop 
   while(m-- && flag){
      flag = false; // marking the flag as false
      // iterating over the string 
      for(int i=0; i<n; i++){
         if(str[i] == '0'){
            if(i == 0){
               if(str[i+1] == '1'){
                  str[i] = '1';
                  flag = true;
               }
            }
            else if(i == n-1){
               if(str[i-1] == '1'){
                  str[i] = '1';
                  flag = true;
               }
            }
            else{
               if((str[i-1] == '1' || str[i+1] == '1') && (str[i-1] != str[i+1])){
                  str[i] = '1';
                  flag = true;
               }
            }
         }
      }
   }
   // returning the string 
   return str;
}
// main function 
int main(){
   string str = "00100";// given string 
   int m = 1000; // given number
   // calling the function to update the string 
   string update_str = updateString(str, m);
   cout<<"The given string "<< str << " after " << m << " number of iterations is "<< update_str<<endl;
   return 0;
}

Output

The given string 00100 after 1000 number of iterations is 11111

Conclusion

In this tutorial, we have implemented a program to find Binary strings by converting all 01 or 10 to 11 after M iterations. We have implemented an approach with the time complexity of O(min(M, N)*N) and O(N) space, where N is the size of the string and M is a given integer (total no of iterations that we have to perform). Also, we have used the flag to reduce the number of iterations if the string is not changing.

Updated on: 16-May-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements