Make Given Binary Strings Equal by Replacing Two Consecutive 0s with Single1 Repeatedly


A binary string in any programming language is a collection of characters, 0 and 1. At every stage, the binary string follows the approach that the string can contain only these two characters.

Consecutive characters in strings are the characters such that the difference in indices is 1. Let us consider two indices, i and j , they are said to be consecutive, if |j-i| = 1.

Two strings in C++ are said to be equivalent if −

  • The corresponding characters in both the strings are same.

  • The length of the strings are equal as well as the characters at corresponding indices coincide.

Some of the examples illustrating the problem statement are as follows −

Sample Example

str1 - “10001”

str2 - “101”

Solution -

str1 cannot be converted to str2, since on conversion of str1 to create str2 equivalent string, we will end up with str1 as “1011”, whereas str2 is “101”.

Example 2 - Let us consider the following input −

str1 - “001”

str2 - “11”

Solution -

str1 can be converted to str2, upon changing two leading zeros with a single 1.

The following problem can be solved using the character matching and string traversal in C++.

Algorithm

  • Step 1 − Two pointers, i and j are used to iterate through the strings, s1 and s2 simultaneously respectively.

  • Step 2 − If the characters at both the indices are matching, we increment the i and j pointers.

  • Step 3 − In case, the characters are not equivalent, we check if the character at the ith and (i+1)th index is ‘0’ and the character at jth index is ‘1’.

  • Step 4 − If such case exists, conversion can be performed. The i pointer is incremented by two positions and j by one index position, since both zeros are converted to one.

  • Step 5 −In case, the characters are not equivalent, and the above case also not exists, conversion is not possible.

  • Step 6 − If both the pointers i and j reach at end positions successfully, the conversion of s1 to s2 is possible.

Example

The following code snippet takes as input two binary strings and checks if these two strings can be made equivalent by simply character replacement according to the specified conditions

//including the required libraries
#include <bits/stdc++.h>
using namespace std;

//convert consecutive 0's to 1
bool convertBinary(string s1, string s2){

   //fetching the lengths of both the strings
   int len1 = s1.length();
   int len2 = s2.length();
   string temp ="";

   //maintaining counters of both the strings
   int i = 0, j = 0;

   //iterating over both the strings simultaneously
   while (i < len1 && j < len2) {

      //if both the characters are equivalent in nature
      //skip to next character
      if (s1[i] == s2[j]) {
         temp+=s1[i];

         //incrementing both pointers
         i++;
         j++;
      }

      // if both characters differ
      else {

         // checking if '00' of s1 can be converted to '1' of s2
         if(s2[j]=='1' && s1[i]=='0'){

            //checking if i+1th index exists and is equivalent to 0
            if(i+1 < len1 && s1[i+1]=='0'){

               //conversion is possible
               //skip two 0's of s1 since converted to 1 in s2
               temp+='1';
               i += 2;
               j++;
            } else {
               return false;
            }
         }

         // If not possible to combine
         else {
            return false;
         }
      }
   }
   cout<<"Entered string2 "<<s2<<"\n";
   cout<<"Converted string1 "<<temp<<"\n";

   //check if both the strings are returned to end position
   if (i == len1 && j == len2)
      return true;
      return false;
}

// calling the conversion rate
int main(){
   string str1 = "100100";
   string str2 = "1111";

   //capturing result
   cout<<"Entered string1 "<<str1<<"\n";
   bool res = convertBinary(str1, str2);
   if (res)
      cout << "First string can be converted to second";
   else
      cout << "First string can't be converted to second";
   return 0;
}

Output

Entered string1 100100
Entered string2 1111
Converted string1 1111
First string can be converted to second

Conclusion

Since, the approach efficiently compared the input strings, character by character, the time complexity is O(min(length of string)). String traversal is an important aspect of problem solving in strings.

Updated on: 15-Mar-2023

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements