Minimum number of deletions and insertions. | Practice | GeeksforGeeks

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
Explanation: 2 deletions and 1 insertion
p and h deleted from heap. Then, p is 
inserted at the beginning One thing to 
note, though p was required yet it was 
removed/deleted first from its position 
and then it is inserted to some other 
position. Thus, p contributes one to the 
deletion_count and one to the 
insertion_count.
Example 2:
Input : str1 = "geeksforgeeks"
str2 = "geeks"
Output: 8
Explanation: 8 deletions

 
Your Task:
You don't need to read or print anything. Your task is to complete the function minOperations() which takes both strings as input parameter and returns the minimum number of operation required.
Expected Time Complexity: O(|str1|*|str2|)
Expected Space Complexity: O(|str1|*|str2|)
Constraints:
1 ≤ |str1|, |str2| ≤ 1000
All the characters are lower case English alphabets
0
apurvkumarak2 days ago
C++ Solution 
 class Solution{
       private : 
    int lcs(string & s1,string & s2,int m,int n,vector<vector<int>> & dp){
        if ( m == 0 || n == 0){
            return 0;
        }
        if(dp[m][n] != -1){
            return dp[m][n];
        }
        if(s1[m-1] == s2[n-1]){
            return 1 + lcs(s1,s2,m-1,n-1,dp);
        }
        int res = max(lcs(s1,s2,m-1,n,dp),lcs(s1,s2,m,n-1,dp));
        dp[m][n]=res;
        return res;
    }
	public:
	int minOperations(string str1, string str2) { 
	    vector<vector<int>> dp(str1.size()+1,vector<int> (str2.size()+1,-1));
	    int res = this->lcs(str1,str2,str1.size(),str2.size(),dp);
	    res = (str1.size() - res) + (str2.size() - res);
	    return res;
	}
};
0
uratabhi2 weeks ago
   int n = str1.size();        int m = str2.size();        std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
   for (int i = 1; i <= n; i++)   {       for (int j = 1; j <= m; j++)       {           if (str1[i - 1] == str2[j - 1])           {               dp[i][j] = 1 + dp[i - 1][j - 1];           }           else           {               dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);           }       }   }    return n+m-2*dp[n][m];
+1
gargharshit5671 month ago
C++ Direct DP Solution withouth doing calculations after lcs
 
 
class Solution{
	public:
	int minOperations(string str1, string str2) 
	{ 
	    // Your code goes here
	    int n= str1.length();
	    int m= str2.length();
	    int dp[n+1][m+1];
	    for(int i=0;i<n+1;i++)dp[i][0]=i;
	    for(int i=1;i<m+1;i++)dp[0][i]=i;
	    
	    for(int i=1;i<n+1;i++){
	        for(int j=1;j<m+1;j++)
	        {
	            if(str1[i-1] == str2[j-1]){
	                dp[i][j]= dp[i-1][j-1];
	            }
	            else
	            {
	                dp[i][j] = min(dp[i-1][j-1]+2, min(dp[i-1][j]+1,dp[i][j-1]+1));
	            }
	        }
	    }
	    return dp[n][m];
	}
};
 
0
shrustis1761 month ago
C++ solution
 
class Solution{public:int lcs(int x, int y, string s1, string s2)   {       // your code here       int t[x+1][y+1];       int i,j;              for(i=0; i<x+1; i++)       for(j=0; j<y+1; j++)           if( i==0 || j==0)               t[i][j]=0;              for(i=1; i<x+1; i++)       for(j=1; j<y+1; j++)       {           if(s1[i-1] == s2[j-1])               t[i][j] = 1+t[i-1][j-1];           else               t[i][j] = max(t[i-1][j], t[i][j-1]);       }              return t[x][y];} int minOperations(string str1, string str2) {     // Your code goes here    int n = str1.length();    int m = str2.length();       int len = lcs(n,m,str1, str2);      int del= n-len;   int ins= m-len;      return del+ins;} };
0
gulshan991 month ago
 simple idea : 
   1 find lcm
   2 deletion will be(str1.length()-lcm +str2.length()-lcm)
	public:
	int minOperations(string str1, string str2) 
	{ 
	    // Your code goes here
	    int n=str1.length(),m=str2.length();
        int dp[n+1][m+1];
        for(int i=0;i<=n;i++)
        {
            dp[i][0]=0;
        }
        for(int j=0;j<=m;j++)
        {
            dp[0][j]=0;
        }
        
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(str1[i-1]==str2[j-1])
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                }
                else{
                    dp[i][j]= max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return n-dp[n][m]+m-dp[n][m];
	    
	} 
0
ayushkumar54511 month ago
 int dp[1001][1001];int del_ins(string &a,string &b,int n,int m){    for(int i=0;i<=n;i++){        for(int j=0;j<=m;j++){            if(i==0 or j==0) dp[i][j]=0;        }    }    for(int i=1;i<n+1;i++){        for(int j=1;j<m+1;j++){            if(a[i-1]==b[j-1]){                dp[i][j]=1+dp[i-1][j-1];            }            else{                dp[i][j]=max(dp[i][j-1],dp[i-1][j]);            }        }    }    return dp[n][m];}int minOperations(string str1, string str2) {     // Your code goes here    int n=str1.size();    int m=str2.size();    int x=del_ins(str1,str2,n,m);    int y=del_ins(str1,str2,n,m);    int k=n-x;    int z=m-y;    return k+z;        } 
0
mailtoamandeepsingh281 month ago
C++ solution
 int minOperations(string str1, string str2) {     if(str1==str2)        return 0;    int n = str1.length();    int m = str2.length();    vector<vector<int>>dp(n+1, vector<int>(m+1, -1));        for(int i=0;i<=n;i++)        dp[i][0] = 0;           for(int i=0;i<=m;i++)        dp[0][i] = 0;        for(int i=1;i<=n;i++){        for(int j=1;j<=m;j++){            if(str1[i-1]==str2[j-1]){                dp[i][j] = 1+dp[i-1][j-1];            }                        else{                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);            }        }    }        int lcs = dp[n][m];        return n+m-2*lcs;}
0
annanyamathur2 months ago
int memo[1001][1001];int matched(string a,string b,int n,int m){ if(memo[n][m]==-1)   { if(n==0 ||m==0)  memo[n][m]=0;  else{if(a[n-1]==b[m-1])  memo[n][m]=1+matched(a,b,n-1,m-1);  else  memo[n][m]= max(matched(a,b,n-1,m),matched(a,b,n,m-1));}}  return memo[n][m];    }int minOperations(string a, string b) {       int n=a.size(), m=b.size();    for(int i=0;i<=n;i++)    {        for(int j=0;j<=m;j++)        {            memo[i][j]=-1;        }    }    int res=matched(a,b,n,m), ans=(n-res)+(m-res);    //cout<<res<<endl;    return ans;    } 
+1
tirtha19025682 months ago
JAVA SOLUTION
 
class Solution
{
public int minOperations(String str1, String str2) 
{ 
    int m = str1.length();
    int n = str2.length();
    
    // LCS
    int[][]dp = new int[m+1][n+1];
    for(int i=1;i<dp.length;i++){
        for(int j=1;j<dp[0].length;j++){
            if(str1.charAt(i-1) == str2.charAt(j-1)){
                dp[i][j] = dp[i-1][j-1]+1;
            }
            else{
                dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
            }
        }
    }
    
    
    int lcs = dp[m][n];
    int insert = n - Math.abs(lcs);
    int delete = m - Math.abs(lcs);
    return insert + delete;
    } 
    
}
0
chessnoobdj2 months ago
C++
int minOperations(string s1, string s2) 
	{
	    int n = s1.size(), m = s2.size();
	    vector <vector<int>> dp(n+1, vector<int> (m+1, 0));
	    for(int i=1; i<=n; i++){
	        for(int j=1; j<=m; j++){
	            if(s1[i-1] == s2[j-1])
	                dp[i][j] = 1 + dp[i-1][j-1];
	            else
	                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
	        }
	    }
	    return n+m-2*(dp[n][m]);
	} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.