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 charactors 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 is same then, check for the next charactors.
- If the characters at the ith index and jth index are not same, then check for the frequency of the str2[j] characters, if frequency is greater than 0 then increment the jth pointer and check for next pair of characters.
- Else we can’t transform string str1 into string str2.
- After all the above iteration if both the pointers reaches 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Largest substring of str2 which is a prefix of str1
- 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]
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Transform the string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Check if it is possible to transform one string to another
- Find Kth largest string from the permutations of the string with two characters
- Find the smallest window in a string containing all characters of another string
- Count of times second string can be formed from the characters of first string
- Transform a string such that it has abcd..z as a subsequence
- Transform One String to Another using Minimum Number of Given Operation
- String matching where one string contains wildcard characters
- Check whether second string can be formed from characters of first string
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.
Improved By : bgangwar59