


Input: 's1' = "abcd", 's2' = "anc"
Output: 3
Explanation:
Here, 's1' = "abcd", 's2' = "anc".
In one operation remove 's1[3]', after this operation 's1' becomes "abc".
In the second operation remove 's1[1]', after this operation 's1' becomes "ac".
In the third operation add 'n' in 's1[1]', after this operation 's1' becomes "anc".
Hence, the minimum operations required will be 3. It can be shown that there's no way to convert s1 into s2 in less than 3 moves.
The first line of the input contains a string 's1'.
The second line of the input contains a string 's2'.
Return the minimum number of operations required to convert string 's1' into 's2'.
You do not need to print anything, it has already been taken care of. Just implement the given function.
The basic idea to solve this problem is that we have to minimize the insertion and deletion operation. For minimizing these operations we want the maximum part of the string unchanged so, we want a longest subsequnce which is common in both of the strings this is known as longest common subsequence(LCS), in the strings “aeaea” and “aaae”, “aae” is the lcs. After we know the lcs we can say that (n-lcs) characters we will remove from str and then m-lcs character we will add so the final answer will be n+m-2*lcs. Now, the only task remain is to find the lcs of the two strings. We will sove it recursively, If we are at ith index in str and at jth index in ptr then we have 2 cases, one is when str[i] is equal to ptr[j], in this case we can say that ith and jth characters are included in the LCS and we can move further.
If str[i] is not equal to ptr[j], in this case we have 2 options whether to take the ith character or jth character in the LCS and take the max of both of the cases.
Lets define a function “lcs(i, j, n, m, str, ptr) where ‘i’ is the index we are at in str, ‘j’ is the index we are at in ptr, ‘n’ is the length of the string str, ‘m’ is the length of the string ptr.
This function returns the LCS of string str[i..n-1] and ptr[j..m-1].
Here is the algorithm:
The basic idea is to store the results of the recursive calls to avoid repetition. We can memorize the results of our function calls in an array (say, ‘DP’).
So,let's first define Dp[i][j]
Dp[i][j] = Longest common subsequence of string str[i..n-1] and ptr[j….m-1].
All the transition are same as discussed in brute force approach.
Here is the algorithm:
The idea is similar to the previous approach. In this approach, we use an iterative way to find longest common subsequence.
Dp[i][j] = Longest common subsequence of string str[0…i] and ptr[0…..j].
If we are at ith index in str and at jth index in ptr then we have 2 cases, one is when str[i] is equal to ptr[j], in this case we can say that ith and jth characters are included in the LCS and we have already calculated the LCS of str[0…i-1] and ptr[0…j-1] then:
Dp[i][j] =1 + dp[i-1][j-1], if str[i] is equal to ptr[j].
If str[i] is not equal to ptr[j], in this case we have 2 options whether to take the ith character or jth character in the LCS and take the max of both of the cases.
Dp[i][j] = max(dp[i-1][j], dp[i][j-1]), if str[i] is not equal to ptr[j].
Here is the algorithm :