Minimum number of flipping adjacent bits required to make given Binary Strings equal


A binary string is a string that contains only two different types of characters 0 and 1. We will be given two binary strings of the same length and our task is to make both of them equal by toggling two adjacent characters of the first string. Also, we have to do this in a minimum number of operations as possible. If it is not possible to convert the first string to the second string then return -1.

Sample Example

Input 1

string1: 101001

string 2: 100110

Output: 2

Explanation − We can toggle the second index character and the adjacent third character of the first string then we will get 100101. Again, we will toggle the 4th and 5th index and get the string equal to the given second string.

Input 2

string 1: 100

string 2: 011

Output: -1

Explanation − There is no way to make both the string equal, we can toggle zeroth and the first index and will get 010 and then we have two indexes the same but the third is different and we can make the conclusion that it is impossible to make the first string equal to second.

Approach

We have seen the examples of the given problem, now let us move to the steps or the approach to implementing the code for the given problem.

  • First, we will create a function that will take both strings as the parameter and will return an integer.

  • The returned value indicates the number of minimum steps required to convert the first string to another or -1 for not possible.

  • In the function, first, we will get the length of the string to traverse over the string.

  • We will create a variable to store the number of steps required to make the strings equal.

  • Using for loop we will traverse over the strings and if the current index has the same character, then we will move to the next character.

  • We will toggle the value of the current index and the next index and will increase the count.

  • As we are moving forward and making the current value equal to the second-string value means both the string are equal until the second last index.

  • If the last index is the same for both strings means that both strings are finally equal and we will return the count.

  • Else the strings are impossible to make equal and we will return -1.

  • From the main function, we will call the function and according to the return value, we will print the result.

Example

#include <iostream>
using namespace std;
// function to check if it is possible to make the string equal 
int makeEqual(string str1, string str2){
   int len = str1.length(); // getting length of the strings 
   int count = 0; // variable to count the number of steps     
   // traversing over the string 
   for(int i=0; i<len-1; i++){
      if(str1[i] == str2[i]){
         // Both the charcters are the same 
         continue;
      }
      else{
         count++;
         // update the current and the next character
         str1[i] = '1' + '0' - str1[i]; // technique to toggle the characters
         str1[i+1] = '1' + '0' -str1[i+1];
      }
   }
   // if the last element is the same means the strings are the same 
   if(str1[len-1] == str2[len-1]){
      return count;
   }
   else{
      return -1;
   }
}
int main(){
   string str1 = "101010"; // given first string 
   string str2 = "100101"; // given second string    
   // calling the function to count the minimum steps 
   // to make the string equal 
   int steps = makeEqual(str1,str2);   
   if(steps == -1){
      cout<<"It is not possible to make string "<<str1<<" equal to the string "<<str2<<endl;
   }
   else{
      cout<<"We can make string "<< str1<<" equal to string "<<str2<< " in "<<steps<<" number of steps"<<endl;
   }
   return 0;
}

Output

We can make string 101010 equal to string 100101 in 2 number of steps

Time and Space Complexity

The time complexity of the above code O(N), where N is the length of the given string. As we are just traversing over the strings only once make the time complexity linear.

The space complexity of the above code is O(1), we are not using any extra space here.

Note − In this problem, we were given that the length of both strings is equal, if not given then we have to add an extra condition in the function to compare the length and if the length is not the same then we have to return -1 or not possible.

Conclusion

In this tutorial, we have implemented an approach to check if we can make convert one given binary string to another given binary string in the minimum move if possible. We have implemented a code with the linear time complexity and constant space complexity which works under the given constrains of the toggle of the adjacent elements.

Updated on: 17-May-2023

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements