Given three strings str1, str2 & str3. The task is to find whether string str1 can be transformed to string str2 by taking characters from str3. If yes then print “YES”, Else print “NO”.
Input: str1 = “abyzf”, str2 = “abgdeyzf”, str3 = “poqgode”.
Remove ‘g’ from str3 and insert it after ‘b’ in str1, A = “abgyzf”, C=”poqode”
Remove ‘d’ from str3 and insert it after ‘g’ in str1, A = “abgdyzf”, C=”poqoe”
Remove ‘e’ from str3 and insert it after ‘d’ in str1, A = “abgdeyzf”, C=”poqo”
Therefore str1 is transform into str2.
Input: A = “abyzf”, B = “abcdeyzf”, C = “popode”.
It is not possible to transform A equal to C.
Approach: This problem can be solved using Greedy Approach.
- Compute the frequency of each character of string str3.
- Traverse the string str1 & str2 using two pointers(say i for str1 and j for str2) simultaneously and do the following:
- If the characters at the ith index of str1 and jth index of str2 are the same then, check for the next characters.
- If the characters at the ith index and jth index are not the same, then check for the frequency of the str2[j] characters, if the frequency is greater than 0 then increment the jth pointer and check for the next pair of characters.
- Else we can’t transform string str1 into string str2.
- After all the above iteration if both the pointers reach the end of the string respectively, then str1 can be transformed into str2.
- Else str1 cannot be transformed into str2.
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
- Check whether str1 can be converted 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
- Minimum cost to convert str1 to str2 with the given operations
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Minimum operations to transform given string to another by moving characters to front or end
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Transform One String to Another using Minimum Number of Given Operation
- Transform the string
- Check if it is possible to transform one string to another
- Transform a string such that it has abcd..z as a subsequence
- Burrows - Wheeler Data Transform Algorithm
- Inverting the Burrows - Wheeler Transform
- Move To Front Data Transform Algorithm
- Inverting the Move to Front Transform
- Python | Convert a list of characters into a string
- Convert a String into a square matrix grid of characters
- Number of ways to insert two pairs of parentheses into a string of N characters
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.