Convert a Binary String to Another by Flipping Prefixes a Minimum Number of Times


Prefixes are the substrings that start from the zeroth index and could be of any size from 1 to the length of the given string. We are given two binary string which means both strings contain only two different types of characters and we have to make the first string equal to the second one by flipping the prefix a minimum number of times. Also, it is given that the length of both of the given strings is equal.

Input 1

string str1 = "01100"
string str2 = "10101" 

Output

3

Explanation

Only operation that we can perform is to choose a prefix of any length and then update its value. So, we can choose the complete string then the string will be, 10011. Now we can choose the prefix of length total length of the string minus-1 and then the string will be 01101. In the end, we can choose the prefix of length 2 and that will give us the answer.

Input 2

 string str1 = “1001” 
 string str2 = “1000” 
    

Output

2

Idea

We can an observation here if we need to change the string by flipping all the characters of chosen prefix.

Here the thing to notice is if we want to change the last bit of the first string, then we have to select the complete string and then there will be no need to choose the complete string later.

So, by this observation, we can traverse over the string from the last bit and will check if the current characters of both the strings are same or not. If they are the same then we move to the next character otherwise we will update our answer and will flip the string.

Now, flipping the complete string is a costly step so instead it’s better to take a variable to mark whether the string is flipped or not.

If the string is flipped once then the characters will change but if we flip the string twice then the string will come back to the same form back.

Steps to Implement the Code

  • First, we will create a main function and will define the strings, and call to the function to get the minimum number of steps required.

  • Our helper function will take both the strings as the parameter and will return an integer value that will be our required number.

  • In the function, we will get the length of the string and will store that in a variable.

  • A boolean variable will be there to store whether the current string is flipped or not.

  • We will traverse over the string using the for loop from the last index to the zeroth index.

  • If the current character for both the strings is the same and the string is flipped or if the characters are not the same and the string is not flipped then we update the answer by one and flip the string.

  • At the end we will return the answer value and will print that in the main function.

Example

#include <bits/stdc++.h>
using namespace std;
// creating a function to find number of steps required 
int stepsReq(string str1, string str2){
   int len = str1.length(); // getting length of the strings
   int ans = 0; // variable to store the answer 
   bool isFlipped = false;
   // traversing over the string 
   for(int i=len-1; i>=0; i--){
      if(str1[i] == str2[i]){
         if(isFlipped){
            // characters are the same but the string is flipped ans++;
            isFlipped = false;
         }
       } else{
          if(!isFlipped){
             // characters are not the same and the string is not flipped 
             isFlipped = true;
             ans++;
          }
       }
   }
   return ans; // return the answer 
}
int main(){
   string str1 = "01100"; // given strings 
   string str2 = "10101";
   // calling the function 
   cout<<"The minimum number of steps required to convert the first string to another by flipping the prefixes is" <<stepsReq(str1,str2)<<endl;
   return 0; 
}

Output

The minimum number of steps required to convert the first string to another by flipping the prefixes is 2

Time and Space Complexity

The time complexity of the above code is O(N), where N is the length of the string.

As we are not going to use any extra space, the time complexity of the above code is O(1).

Note

In this problem, we are given the length of both the strings the same so we didn't write any condition for that, but if is not given then we have to put a condition for that and check if the lengths are not the same then return is not possible.

Conclusion

In this tutorial, we have implemented a program to convert a Binary String to another by flipping prefixes a minimum number of times. The prefixes are the substrings that start from the zeroth index and could be of any size from 1 to the length of the given string. We have implemented a program that works with O(N) time complexity and O(1) space complexity.

Updated on: 11-Jul-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements