
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Two Binary Strings Can Be Made Equal by Bitwise XOR of Adjacent
In this problem, we will check whether we can convert string alpha2 to alpha1 by performing the XOR operation of any two adjacent characters and replacing both characters with an XOR value.
We will use the logic based on the XOR value of two digits to solve the problem. If the string contains at least one ?1' and more than two adjacent consecutive characters, we can convert the alpha2 string to alpha1.
Problem statement ? We have given two binary strings of the same length. We need to check whether we can convert the alpha2 string to alpha1 by performing the below operations on the alpha2 strings.
Take any two adjacent digits, take the XOR of both, and replace both digits with the XOR result. We need to do Cp = Cp XOR Cp + 1 or Cp + 1 = Cp XOR Cp + 1
Sample examples
Input
alpha1 = "00101"; alpha2 = "00110";
Output
Yes
Explanation
Take the XOR of the 2nd and 3rd characters in the first operation. So, alpha2 will be 01110.
Take XOR of the 4th and 5th characters. The updated string will be 01111.
Take XOR of the 3rd and 4th characters. The updated alpha2 string will be 01001.
Take XOR of the 2nd and 3rd characters. The updated string will be 01101.
Take the XOR of 1st and 2nd characters. The string will be 11101.
Take the XOR of the 1st and 2nd characters again; the resultant string will be 00101, equal to the alpha1 string.
Input
alpha1 = "00000"; alpha2 = "00110";
Output
Yes
Explanation ? We can take the XOR of the 2nd and 3rd characters of the alpha2 string, and the updated string will be 00000.
Input
alpha1 = "00110"; alpha2 = "00000";
Output
No
Explanation ? We can't generate 11 by taking the XOR of any two characters of the alpha2 string. So, it is not possible to convert the alpha2 string to the alpha1 string.
Approach 1
Let's observe the XOR output of all possible adjacent characters.
00 converted to 00
11 converted to 00
01 converted to 11
10 converted to 11
So, If alpha2 contains all ?0', we can't make the same as the alpha1 string, except alpha1 contains all ?0'.
Otherwise, if alpha2 has at least one ?1', and alpha1 has at least one pair of the same consecutive elements, we can convert the alpha2 string to the alpha1 string.
Algorithm
Step 1 ? Initialize the ?cnt1' and ?cnt2' with 0 to store a count of ?1' in both strings.
Step 2 ? If both strings are already the same, return true.
Step 3 ? Traverse both strings and count a number of ?1' in both strings.
Step 4 ? If alpha1 contains all ?0', and alpha2 string contains at least one ?1', return false.
Step 5 ? Initialize the ?cnt' with 0 to store the count of different consecutive adjacent pairs.
Step 6 ? Traverse the string, and if the current character is not equal to the previous character, increment the ?cnt' value by 1.
Step 7 ? If the ?cnt' value equals string length ? 1, return false, as the string doesn't contain the same consecutive characters.
Step 8 ? Else, return true.
Example
#include <bits/stdc++.h> using namespace std; bool checkForEquality(string alpha1, string alpha2, int len) { int cnt1 = 0; int cnt2 = 0; // When strings are already same if (alpha2 == alpha1) { return true; } // Counting the number of 1's in both strings for (int p = 0; p < len; p++) { if (alpha2[p] == '1') { cnt1++; } if (alpha1[p] == '1') { cnt2++; } } // Corner case if (cnt1 == 0 && cnt2 > 0) { return false; } int cnt = 0; // Count different adjacent characters for (int p = 0; p < len - 1; p++) { if (alpha1[p] != alpha1[p + 1]) { cnt++; } } // When all characters are different adjacent characters, we can't make strings equal if (cnt == len - 1) { return false; } else { return true; } } int main() { string alpha1 = "00101"; string alpha2 = "00110"; int len = alpha1.length(); if (checkForEquality(alpha1, alpha2, len)) { cout << "Yes, It is possible to convert alpha1 to alpha2"; } else { cout << "No, It is not possible to convert alpha1 to alpha2"; } return 0; }
Output
Yes, It is possible to convert alpha1 to alpha2
Time complexity ? O(N) for traversing the string.
Space complexity ? O(1), as we don't use dynamic space.
We learned to convert the alpha2 string to the alpha1 string by performing the XOR operation of the adjacent characters. In some problems, it is necessary to find the special logic for solving the problem, as we used alpha1 should contain at least one ?1', and alpha2 should contain at least 1 same consecutive element, which we can find using the observation.