Edit Distance | Practice | GeeksforGeeks

Given two strings s and t. Return the minimum number of operations required to convert s to t.
The possible operations are permitted:

Insert a character at any position of the string.
Remove any character from the string.
Replace any character from the string with any other character.

Insert a character at any position of the string.
Remove any character from the string.
Replace any character from the string with any other character.
 
Example 1:
Input: 
s = "geek", t = "gesek"
Output: 1
Explanation: One operation is required 
inserting 's' between two 'e's of str1.

Example 2:
Input : 
s = "gfg", t = "gfg"
Output: 
0
Explanation: Both strings are same.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function editDistance() which takes strings s and t as input parameters and returns the minimum number of operation to convert the string s to string t. 

Expected Time Complexity: O(|s|*|t|)
Expected Space Complexity: O(|s|*|t|)

Constraints:
1 ≤ Length of both strings ≤ 100
Both the strings are in lowercase.
0
santoshkumar15novmth1 day ago
private:    int helper(int i, int j, string s1, string s2, vector<vector<int>> &dp) {       //base case       if(i < 0) return j+1;       if(j < 0) return i+1;       if(dp[i][j]!=-1) return dp[i][j];              if(s1[i] == s2[j]) return dp[i][j]=helper(i-1, j-1, s1, s2, dp);       return dp[i][j]=1 + min(helper(i-1, j, s1, s2, dp), min(helper(i, j-1, s1, s2, dp),                                                      helper(i-1, j-1, s1,s2,dp)));   } public:   int editDistance(string s, string t) {       int n=s.size();       int m=t.size();       vector<vector<int>> dp(n, vector<int>(m, -1));       return helper(n-1, m-1, s, t, dp);   }
0
aniketsaraswat1122 weeks ago
int editDistance(string s, string t){
        int n = s.size(), m = t.size();
        int dp[n+1][m+1] = {};
        for(int i = 0; i<=n; i++) dp[i][0] = i;
        for(int i = 0; i<=m; i++) dp[0][i] = i;
        for(int i = 1; i<=n; i++){
            for(int j = 1; j<=m; j++){
                if(s[i-1] != t[j-1])
                    dp[i][j] = min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1])) + 1;
                else
                    dp[i][j] = dp[i-1][j-1];
            }
        }
        return dp[n][m];
    }
+1
kshitij14102 weeks ago
int editDistance(string s, string t) {        int n=s.size();    int m= t.size();    vector<int>prev(m+1),curr(m+1);        for(int j=0;j<=m;j++)    prev[j]=j;        for(int i=1;i<=n;i++)    {        curr[0]=i;        for(int j=1;j<=m;j++)        {            if(s[i-1]==t[j-1])            curr[j]=prev[j-1];            else{                curr[j]=1+min(curr[j-1],min(prev[j],prev[j-1]));            }        }                prev=curr;    }        int ans=prev[m];    prev.clear();    curr.clear();    return ans;   }
0
joeldcostaec193 weeks ago
class Solution {   public int editDistance(String s, String t) {       int x=t.length();       int y=s.length();       int[][] dp=new int[x+1][y+1];       dp[0][y]=x;       dp[x][0]=y;       for(int i=1;i<=x;i++){           dp[i][y]=dp[i-1][y]-1;       }       for(int i=1;i<=y;i++){           dp[x][i]=dp[x][i-1]-1;       }       for(int i=x-1;i>=0;i--){           for(int j=y-1;j>=0;j--){               if(t.charAt(i)!=s.charAt(j)){               dp[i][j]=1+Math.min(dp[i+1][j],dp[i][j+1]);               dp[i][j]=Math.min(dp[i][j],1+dp[i+1][j+1]);               }               else               dp[i][j]=dp[i+1][j+1];           }       }       return dp[0][0];       // Code here   }}
0
robbinhackk3 weeks ago
int memo[101][101]; int fun(string s1, string s2, int m, int n){     if (m == 0)       return n;   if (n == 0)       return m;    if (memo[m][n] != -1)       return memo[m][n];           else   {       if (s1[m - 1] == s2[n - 1])           memo[m][n]= fun(s1, s2, m - 1, n - 1);       else       {          // memo[m][n]= 1 +min(fun(s1, s2, m, n - 1),fun(s1, s2, m - 1, n),fun(s1, s2, m - 1, n - 1));           memo[m][n]= 1 + min(fun(s1, s2, m, n - 1), min(fun(s1, s2, m - 1, n), fun(s1, s2, m - 1, n - 1)));       }   }   return memo[m][n];}   int editDistance(string s, string t) {       int m = s.length(), n = t.length();       memset(memo,-1,sizeof(memo));       return fun(s, t, m, n);   }
0
jainmuskan5654 weeks ago
int editDistance(string s, string t) {       int len1= s.length();       int len2= t.length();       int dp[len1+1][len2+1];       for(int i=0;i<len1+1;i++){           for(int j=0;j<len2+1;j++){               //Base case when S len is 0               if(i==0){                   dp[i][j]= j;                                                                                                                                                       }               // if t len is 0               else if(j==0){                   dp[i][j]=i;               }           }       }       for(int i=1;i<len1+1;i++){           for(int j=1;j<len2+1;j++){                                      //same char so we just took the diagonal element               if(s[i-1]==t[j-1]){                   dp[i][j]= dp[i-1][j-1];               }               // different then 3 case insert, replace, remove               else{                   dp[i][j]= 1+min(dp[i][j-1], min(dp[i-1][j-1], dp[i-1][j]));               }           }       }       return dp[len1][len2];   }
0
adityagagtiwari1 month ago
Easy java solution.
class Solution {   public int editDistance(String s, String t) {       // Code here   int[][] dp = new int[s.length()+1][t.length()+1];      for(int i=0;i<s.length()+1;i++)       {           for(int j=0;j<t.length()+1;j++)           {               if(i==0) dp[i][j] = j;               if(j==0) dp[i][j] = i;           }       }       for(int i=1;i<dp.length;i++)       {           for(int j=1;j<dp[0].length;j++)           {               //if the character are already equal then it will check for its i-1 and j-1                //characters as for ith character no extra moves will be required               if(s.charAt(i-1)==t.charAt(j-1))               {                   dp[i][j] = dp[i-1][j-1];               }               //from all three states i.e. dp[i-1][j-1],dp[i-1][j],dp[i][j-1] its possible                // to reach dp[i][j] by performing the three operations hence we will take the                //smallest among the three and add 1 to it for the current operation               //as the two chracters arent equal               else               {                   dp[i][j] = 1 + Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));               }           }       }       return dp[dp.length-1][dp[0].length-1];   }}
0
pujagulhane20011 month ago
int editDistance(string s, string t) {       // Code here     int x=s.length();     int y=t.length();       int m[x+1][y+1];     for(int i=0;i<x+1;i++) {       for(int j=0;j<y+1;j++) {           if(i==0) m[0][j]=j;           else if(j==0) m[i][0]=i;       }   }   for(int i=1;i<x+1;i++) {       for(int j=1;j<y+1;j++) {           if(s[i-1]==t[j-1])  {                m[i][j] = m[i-1][j-1];              }else{                                    m[i][j] = 1+min(m[i-1][j] , min(m[i-1][j-1],m[i][j-1]));           }       }   }   return m[x][y];            
+1
anandkumarsatapathy1 month ago
int longestCommonSubsequence(string s, string t) {       int n = s.length();       int m = t.length();               vector<vector<int>>dp(n+1,vector<int>(m+1,0));                      //  making the diagonal the of the zero ;           for(int i = 0;i<=n;i++)dp[i][0]=i;           for(int j =0;j<=m;j++)dp[0][j]=j;                      // this line for the string matching                  for(int i = 1;i<=n;i++){               for(int j = 1;j<=m;j++){                   if(s[i-1]==t[j-1])                   {                       dp[i][j]=dp[i-1][j-1];                                          }                   else                   {                       dp[i][j]=1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));                   }               }           }       return dp[n][m];   }   int editDistance(string s, string t) {              return longestCommonSubsequence(s,t);   }
 
0
sandeep55211 month ago
  1-D array space optimized C++ Solution.
int minDistance(string a, string b) {
       int m=a.size(),n=b.size();
       vector<int> f(n+1),s(n+1);
       for(int i=0;i<=n;i++) f[i]=i;
       for(int i=1;i<=m;i++){
           s[0]=i;
           for(int j=1;j<=n;j++){
               if(a[i-1]==b[j-1]) s[j]=f[j-1];
               else s[j]=1+min(min(f[j-1],f[j]),s[j-1]);
           }
           f=s;
       }
       return f[n];
   }
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.