Given two strings A and B, the task is to convert A to B if possible. The only operation allowed is to put any character from A and insert it at front. Find if it’s possible to convert the string. If yes, then output minimum no. of operations required for transformation.
Input: A = "ABD", B = "BAD" Output: 1 Explanation: Pick B and insert it at front. Input: A = "EACBD", B = "EABCD" Output: 3 Explanation: Pick B and insert at front, EACBD => BEACD Pick A and insert at front, BEACD => ABECD Pick E and insert at front, ABECD => EABCD
Checking whether a string can be transformed to another is simple. We need to check whether both strings have same number of characters and same set of characters. This can be easily done by creating a count array for first string and checking if second string has same count of every character.
How to find minimum number of operations when we are sure that we can transform A to B? The idea is to start matching from last characters of both strings. If last characters match, then our task reduces to n-1 characters. If last characters don’t match, then find the position of B’s mismatching character in A. The difference between two positions indicates that these many characters of A must be moved before current character of A.
Below is complete algorithm.
1) Find if A can be transformed to B or not by first creating a count array for all characters of A, then checking with B if B has same count for every character.
2) Initialize result as 0.
2) Start traversing from end of both strings.
……a) If current characters of A and B match, i.e., A[i] == B[j]
………then do i = i-1 and j = j-1
……b) If current characters don’t match, then search B[j] in remaining
………A. While searching, keep incrementing result as these characters
………must be moved ahead for A to B transformation.
Below are the implementations based on this idea.
Minimum number of operations required is 3
Time Complexity: O(n), please note that i is always decremented (in while loop and in if), and the for loop starts from n-1 and runs while i >= 0.
Thanks to Gaurav Ahirwar for above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Check if it is possible to transform one string to another
- Minimum operations to transform given string to another by moving characters to front or end
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Largest even number possible by using one swap operation in given number
- Check if one string can be converted to other using given operation
- Minimum number of subsequences required to convert one string to another
- Minimum swaps required to convert one binary string to another
- Minimum cost to traverse from one index to another in the String
- Minimum number of given operations required to convert a string to another string
- Form the smallest number using at most one swap operation
- Next higher number using atmost one swap operation
- Transform string str1 into str2 by taking characters from string str3
- Find length of longest subsequence of one string which is substring of another string
- Check if it is possible to convert one string into another with given constraints
- Reduce the string to minimum length with the given operation
- Transform the string
- Transform a string such that it has abcd..z as a subsequence
- Maximum length prefix of one string that occurs as subsequence in another
- Print array of strings in sorted order without copying one string into another
- Map every character of one string to another such that all occurrences are mapped to the same character