Make Binary Strings Equal by Replacing 2nd Bit Repeatedly


In this problem, we need to convert the bin1 string to the bin2 string by replacing the second character of the bin1 string with the minimum or maximum of the first and second characters and removing the first character.

As we need to remove the initial character, we need to ensure that the last len2 − 1 character are the same in both strings. Also, we need to ensure that we can get the first character of the second string by performing the given operation with the starting characters of the bin1 string.

Problem statement − We have given bin1 and bin2 binary strings of the len1 and len2 lengths, respectively. We need to check whether we can convert the bin1 string to the bin2 string by following the below operations.

  • Update the second character of the bin1 string with either the minimum or maximum of the first and second characters of the bin1 string.

  • Remove the first character of the bin1 string, and the string size will decrease by 1 in each operation.

Sample examples

Input

bin1 = "0101011"; bin2 = "011";

Output

Yes

Explanation− We can perform the below operations to convert the bin1 string to the bin2 string.

  • We can replace the second character with min(0,1) and remove the first character. So, the string becomes 001011.

  • Again, we perform the same operation, and the string becomes 01011.

  • In the next few operations string becomes 0011 and 011, respectively.

Input

bin1 = "1110"; bin2 = "1110";

Output

Yes

Explanation − The given strings are already same.

Input

bin1 = "101101"; bin2 = "1110";

Output

No

Explanation − We can’t convert the bin1 string to the bin2 string by performing the given operations.

Approach 1

If the length of the bin1 string is smaller, we can’t convert it to the bin2 string.

In other cases, the last len2 − 1 character of the bin1 string remains the same, as we don’t perform any operation on that. So, the last len2 − 1 characters should be the same in both strings.

Also, if the first character of the bin2 string is ‘0’, we should perform the min() operation on starting characters of the bin1 string, and it should contain at least one ‘0’.

If the first character is ‘1’ in the bin2 string, we should perform the max() operation on starting characters of the bin2 string, and it should contain at least one ‘1’.

Algorithm

Step 1 − If the length of bin1 is less than the bin2 string’s length, return false.

Step 2 − Start traversing the bin2 string from the second position.

Step 3 − If bin2[p] is not equal to bin1[p + len1 − len2], return false, as the last len2 −1 characters are not the same.

Step 4 − Traverse first len1 − len2 + 1 character to check whether it contains bin2[0] character. If yes, return true.

Step 5 − Return false at the end of the function.

Example

#include <bits/stdc++.h>
using namespace std;

bool convertAtoB(string bin1, string bin2) {
    int len1 = bin1.size(), len2 = bin2.size();
    // When length 1 is less than length 2
    if (len1 < len2) {
        return false;
    }
    // Check whether substring bin1[p + len1 - len2]... bin1[len1] and bin2[1]... bin2[len2]
    for (int p = 1; p < len2; p++) {
        if (bin1[p + len1 - len2] != bin2[p]) {
            return false;
        }
    }
    // Check whether substring bin1[0... len1 - len2 - 1] contains bin2[0]
    for (int p = 0; p < len1 - len2 + 1; p++) {
        if (bin1[p] == bin2[0]) {
            return true;
        }
    }
    return false;
}
int main() {
    string bin1 = "0101011";
    string bin2 = "011";
    bool res = convertAtoB(bin1, bin2);
    if (res == true) {
        cout << "YES, It is possible to convert bin1 to bin2.";
    } else {
        cout << "NO, It is not possible to convert bin1 to bin2.";
    }
}

Output

YES, It is possible to convert bin1 to bin2.

Time complexity − O(N) to match the string characters.

Space complexity − O(1) as we don’t use any dynamic space.

We learned to convert the first binary string to the second binary string by following the given operations. Programmers may try to solve the problem to check whether a string can be converted to another string by replacing the last−second character with either minimum or maximum of the last and last−second character and removing the last character.

Updated on: 17-Jul-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements