Open In App

Make given Binary Strings equal by replacing two consecutive 0s with single 1 repeatedly

Given two binary strings str1 and str2, the task is to check whether it is possible to convert str1 to str2 by combining two consecutive 0’s into a single 1 repeatedly. 

Examples: 

Input: str1 = “00100”, str2 = “111” 
Output: Yes 
Explanation: Combine first two zeros to one and combine last two zeros to one.

Input: str1 = “00”, str2 = “000” 
Output: No 
Explanation: It is not possible to convert str1 to str2. 

Approach: Let’s process str1 and str2 character by character from left to right in parallel. Let’s use two indices i and j: the index i is for str1 and the index j is for str2. Now, there are two cases: 

  1. If str1[i] = str2[j] then increment both i and j.
  2. If str1[i] != str2[j] then, 
    • If there are two consecutive 0’s in str1 i.e. str1[i] = 0 and str1[i + 1] = 0 and str2[j] = 1 which means both the zeroes can be combined to match with the 1 in str2. So, increment i by 2 and j by 1.
    • Else str1 cannot be converted to str2.
  3. If in the end both i and j are at the end of their respective strings i.e. str1 and str2 then the answer is Yes else the answer is No.

Below is the implementation of the above approach: 

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Function that returns true if str1 can be
// converted to str2 with the given operations
bool canConvert(string str1, string str2)
{
    int i = 0, j = 0;

    // Traverse from left to right
    while (i < str1.size() && j < str2.size()) {

        // If the two characters do not match
        if (str1[i] != str2[j]) {

            // If possible to combine
            if (str1[i] == '0' && str2[j] == '1'
                && i + 1 < str1.size()
                && str1[i + 1] == '0') {
                i += 2;
                j++;
            }

            // If not possible to combine
            else {
                return false;
            }
        }

        // If the two characters match
        else {
            i++;
            j++;
        }
    }

    // If possible to convert one string to another
    if (i == str1.size() && j == str2.size())
        return true;
    return false;
}

// Driver code
int main()
{
    string str1 = "00100", str2 = "111";

    if (canConvert(str1, str2))
        cout << "Yes";
    else
        cout << "No";

    return 0;
}
// Java implementation of the approach
class GFG {

    // Function that returns true if str1 can be
    // converted to str2 with the given operations
    static boolean canConvert(String str1, String str2)
    {
        int i = 0, j = 0;

        // Traverse from left to right
        while (i < str1.length() && j < str2.length()) {

            // If the two characters do not match
            if (str1.charAt(i) != str2.charAt(j)) {

                // If possible to combine
                if (str1.charAt(i) == '0'
                    && str2.charAt(j) == '1'
                    && i + 1 < str1.length()
                    && str1.charAt(i + 1) == '0') {
                    i += 2;
                    j++;
                }

                // If not possible to combine
                else {
                    return false;
                }
            }

            // If the two characters match
            else {
                i++;
                j++;
            }
        }

        // If possible to convert one string to another
        if (i == str1.length() && j == str2.length())
            return true;
        return false;
    }

    // Driver code
    public static void main(String[] args)
    {

        String str1 = "00100", str2 = "111";

        if (canConvert(str1, str2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

// This code contributed by Rajput-Ji
# Python implementation of the approach

# Function that returns true if str1 can be
# converted to str2 with the given operations


def canConvert(str1, str2):
    i, j = 0, 0

    # Traverse from left to right
    while (i < len(str1) and j < len(str2)):

        # If the two characters do not match
        if (str1[i] != str2[j]):

            # If possible to combine
            if (str1[i] == '0' and str2[j] == '1'
                and i + 1 < len(str1)
                    and str1[i + 1] == '0'):
                i += 2
                j += 1

            # If not possible to combine
            else:
                return False
        # If the two characters match
        else:
            i += 1
            j += 1

    # If possible to convert one string to another
    if (i == len(str1) and j == len(str2)):
        return True
    return False


# Driver code
str1 = "00100"
str2 = "111"

if (canConvert(str1, str2)):
    print("Yes")
else:
    print("No")

# This code is contributed by 29AjayKumar
// C# implementation of the approach
using System;

class GFG {

    // Function that returns true if str1 can be
    // converted to str2 with the given operations
    static bool canConvert(string str1, string str2)
    {
        int i = 0, j = 0;

        // Traverse from left to right
        while (i < str1.Length && j < str2.Length) {

            // If the two characters do not match
            if (str1[i] != str2[j]) {

                // If possible to combine
                if (str1[i] == '0' && str2[j] == '1'
                    && i + 1 < str1.Length
                    && str1[i + 1] == '0') {
                    i += 2;
                    j++;
                }

                // If not possible to combine
                else {
                    return false;
                }
            }

            // If the two characters match
            else {
                i++;
                j++;
            }
        }

        // If possible to convert one string to another
        if (i == str1.Length && j == str2.Length)
            return true;
        return false;
    }

    // Driver code
    public static void Main()
    {

        string str1 = "00100", str2 = "111";

        if (canConvert(str1, str2))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}

// This code is contributed by AnkitRai01
<script>
      // JavaScript implementation of the approach
      // Function that returns true if str1 can be
      // converted to str2 with the given operations
      function canConvert(str1, str2) {
        var i = 0,
          j = 0;

        // Traverse from left to right
        while (i < str1.length && j < str2.length) {
          // If the two characters do not match
          if (str1[i] !== str2[j]) {
            // If possible to combine
            if (
              str1[i] === "0" &&
              str2[j] === "1" &&
              i + 1 < str1.length &&
              str1[i + 1] === "0"
            ) {
              i += 2;
              j++;
            }

            // If not possible to combine
            else {
              return false;
            }
          }

          // If the two characters match
          else {
            i++;
            j++;
          }
        }

        // If possible to convert one string to another
        if (i === str1.length && j === str2.length) return true;
        return false;
      }

      // Driver code
      var str1 = "00100",
        str2 = "111";

      if (canConvert(str1, str2)) document.write("Yes");
      else document.write("No");
    </script>
Output
Yes

Time Complexity: O(max(n1, n2)), where n1 and n2 are the lengths of the two strings respectively.
Auxiliary Space: O(1)

Article Tags :