Given two binary strings str1 and str2. The task is to check whether it is possible to convert str1 to str2 by applying the below operation an arbitrary number of times on str1.
In each operation, one can combine two consecutive 0’s into a single 1.
Input: str1 = “00100”, str2 = “111”
Combine first two zeros to one and combine last two zeros to one.
Input: str1 = “00”, str2 = “000”
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:
- If str1[i] = str2[j] then increment both i and j.
- 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.
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of characters in str1 such that after deleting anyone of them str1 becomes str2
- Minimum cost to convert str1 to str2 with the given operations
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Largest substring of str2 which is a prefix of str1
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Transform string str1 into str2 by taking characters from string str3
- Check if a given string can be converted to another by given possible swaps
- Check if one string can be converted to other using given operation
- Check if a string can be converted to another by swapping of adjacent characters of given type
- Check if a String can be converted to Pangram in K changes
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if one string can be converted to another
- Check whether given string can be generated after concatenating given strings
- Sort an Array alphabetically when each number is converted into words
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check whether the string S1 can be made equal to S2 with the given operation
- Check whether two strings can be made equal by increasing prefixes
- Check whether an array can be made strictly increasing by modifying atmost one element
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Check whether two strings can be made equal by copying their characters with the adjacent ones
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.