Given two binary strings. The task is to check if string s1 can be converted to string s2 by performing the given operations any number of times.
- Choose any two adjacent characters in a string s1 and replace one of them by a^b and the other by a b (a OR b).
Input: S1 = “11”, S2 = “10”
Select two adjacent characters and replace s2 by s1s1 and
change s2 by s1^s1
Input: S1 = “000”, S2 = “101”
Approach: Given below is a table which explains all the possibilities of XOR and OR operations.
If the both the string consists of 0’s only and their length is same, conversion is possible, as two adjacent zero will result in zeros only, irrespective of the operation done on it. If the both the string have 1’s, follow the steps below to check if String1 can be converted to String2.
- Check if lengths are equal or not
- Check if both the strings have a minimum of one 1, as all the conversions are possible if both the strings have atleast 1 which can be seen in the table
If both of the above conditions are true, it is possible to convert String1 can be converted to String2.
Below is the implementation of above approach:
Time Complexity: O(n) where n is length of input strings.
- An in-place algorithm for String Transformation
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Find the longest sub-string which is prefix, suffix and also present inside the string
- String Range Queries to find the number of subsets equal to a given String
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Minimal moves to form a string by adding characters or appending string itself
- String slicing in Python to check if a string can become empty by recursive deletion
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Find the character in first string that is present at minimum index in second string
- Find the count of palindromic sub-string of a string in its sorted form
- Find length of longest subsequence of one string which is substring of another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.