DEV Community

Prashant Mishra
Prashant Mishra

Posted on • Originally published at practice.geeksforgeeks.org

1 1

Minimum number of Insertion and deletion needed to convert String A to String B or make both the strings equal (same as lcs)

Problem:
Given two strings str1 and str2. The task is to remove or insert the minimum number of characters from/in str1 so as to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.

Example 1:

Input: str1 = "heap", str2 = "pea"
Output: 3
Enter fullscreen mode Exit fullscreen mode

Solution:

Intuition : Find the largest common subsequence of the two strings and substract it from 
the length of the two strings and finally add the subtractions. i.e. m+n - 2 *lcs(StringA, StringB)
Enter fullscreen mode Exit fullscreen mode

Bottom up Approach(Memoization) :
Time complexity : O(m*n) where is m and n is the length of two strings a and b
space complexity : o(m*n) for using 2d dp and O(m+n) for auxiliary stack space because in worst case we will make m+n recursive calls.

Coding Ninjas Problem

import java.util.*;
public class Solution {
    public static int canYouMake(String str, String ptr) {
        // Write your code here.
        // we will use bottom up approach for solving this problem.
        int dp[][] =new int[str.length()][ptr.length()];
        for(int row[] : dp){
            Arrays.fill(row,-1);
        }
        return str.length()+ptr.length() - 2 *lcs(str,ptr, str.length()-1, ptr.length()-1,dp);
    }
    public static int lcs(String str, String ptr, int m,int n,int dp[][]){
        if(m<0 || n<0){
            return 0;
        }
        if(dp[m][n]!=-1) return dp[m][n];
        if(str.charAt(m)==ptr.charAt(n)){
            dp[m][n] = 1 + lcs(str,ptr,m-1,n-1,dp);
        }
        else{
            dp[m][n] = 0 +  Integer.max(lcs(str,ptr,m-1,n,dp), lcs(str,ptr,m,n-1,dp));
        }
        return dp[m][n];
    }

}
Enter fullscreen mode Exit fullscreen mode

Top Down Approach(Tabulation) :
Time complexity : O(m*n) where is m and n is the length of two strings a and b
space complexity : o(m*n) for using 2d dp

GeeksForGeeksProblem

class Solution
{
    public int minOperations(String str1, String str2) 
    { 
       // we will use top down approach to solve this 
       int dp[][] =new int[str1.length()+1][str2.length()+1];
       for(int i =0;i<=str1.length();i++){
           dp[i][0] = 0;
       }
       for(int i =0;i<=str2.length();i++){
           dp[0][i] = 0;
       }
       for(int i =1;i<=str1.length();i++){
           for(int j =1;j<=str2.length();j++){
               if(str1.charAt(i-1)==str2.charAt(j-1)){
                   dp[i][j] = 1 + dp[i-1][j-1];
               }
               else dp[i][j] = Integer.max(dp[i-1][j],dp[i][j-1]);
           }
       }
       return str1.length() + str2.length() - 2 * dp[str1.length()][str2.length()];
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.