Given two strings s1 and s2 and below operations that can be performed on s1. The task is to find the minimum number of edits (operations) to convert 's1' into 's2'.
Insert: Insert any character before or after any index of s1.
Remove: Remove a character of s1.
Replace: Replace a character at any index of s1 with some other character.
Note: All of the above operations are of equal cost.
Examples:
Input: s1 = "geek", s2 = "gesek" Output: 1 Explanation: We can convert s1 into s2 by inserting an 's' between two consecutive 'e' in s1.
Input: s1 = "gfg", s2 = "gfg" Output: 0 Explanation: Both strings are same.
Input: s1 = "GEEXSFRGEEKKS" and s2 = "GEEKSFORGEEKS" Output: 3 Explanation: Operation 1: Replace 'X' to 'K' Operation 2: Insert 'O' between 'F' and 'R' Operation 3: Remove second last character i.e. 'K' Refer the below image for better understanding.
[Naive Approach] Recursion - O(3 ^ (m + n)) Time and O(m * n) Space
The idea is to process all characters one by one starting from either from left or right sides of both strings. Here we process from the right end of the strings, there are two possibilities for every pair of characters being traversed,
If last characters of both string match, then recursively calculate the answer for rest of part of the strings.
When last characters do not match, we perform all three operations to match, i.e. insert, replace, and remove. And then recursively calculate the result for the remaining part of the string. Upon completion of these operations, we select the minimum and add 1 to it.
Recurrence Relation
Case 1: When the last character of both the strings are same. editDistance(s1, s2, m, n) = editDistance(s1, s2, m-1, n-1)
Case 2: When the last characters are different editDistance(s1, s2, m, n) = 1 + Minimum{ editDistance(s1, s2 ,m,n-1), editDistance(s1, s2 ,m-1, n), editDistance(s1, s2 , m-1, n-1)}
Base Case
Case 1: When s1 becomes empty i.e. m = 0, return n, as it require n insertions to convert an empty string to s2 of size n
Case 2: When s2 becomes empty i.e. n = 0, return m, as it require m removals to convert s1 of size m to an empty string.
Below is the recursive tree for this problem considering the case when the last characters never match.
C++
// A Naive recursive C++ program to find minimum number// of operations to convert s1 to s2#include<iostream>#include<string>#include<algorithm>usingnamespacestd;// Recursive function to find number of operations // needed to convert s1 into s2.inteditDistRec(string&s1,string&s2,intm,intn){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]==s2[n-1])returneditDistRec(s1,s2,m-1,n-1);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.return1+min({editDistRec(s1,s2,m,n-1),editDistRec(s1,s2,m-1,n),editDistRec(s1,s2,m-1,n-1)});}// Wrapper function to initiate the recursive calculationinteditDistance(string&s1,string&s2){returneditDistRec(s1,s2,s1.length(),s2.length());}intmain(){strings1="abcd";strings2="bcfe";cout<<editDistance(s1,s2);return0;}
C
#include<stdio.h>#include<string.h>// Function to find minimum of three numbersintmin(intx,inty,intz){return(x<y)?(x<z?x:z):(y<z?y:z);}// A Naive recursive C program to find minimum number// of operations to convert s1 to s2inteditDistRec(char*s1,char*s2,intm,intn){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]==s2[n-1])returneditDistRec(s1,s2,m-1,n-1);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.return1+min(editDistRec(s1,s2,m,n-1),// InserteditDistRec(s1,s2,m-1,n),// RemoveeditDistRec(s1,s2,m-1,n-1)// Replace);}intmain(){chars1[]="abcd";chars2[]="bcfe";printf("%d\n",editDistRec(s1,s2,strlen(s1),strlen(s2)));return0;}
Java
// A Naive recursive Java program to find minimum number// of operations to convert s1 to s2.classGfG{// Recursive function to find number of operations // needed to convert s1 into s2.staticinteditDistRec(Strings1,Strings2,intm,intn){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1.charAt(m-1)==s2.charAt(n-1))returneditDistRec(s1,s2,m-1,n-1);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.return1+Math.min(Math.min(editDistRec(s1,s2,m,n-1),editDistRec(s1,s2,m-1,n)),editDistRec(s1,s2,m-1,n-1));}// Wrapper function to initiate the recursive calculationstaticinteditDistance(Strings1,Strings2){returneditDistRec(s1,s2,s1.length(),s2.length());}publicstaticvoidmain(String[]args){Strings1="abcd";Strings2="bcfe";System.out.println(editDistance(s1,s2));}}
Python
# A Naive recursive Python program to find minimum number# of operations to convert s1 to s2.# Recursive function to find number of operations# needed to convert s1 into s2.defeditDistRec(s1,s2,m,n):# If first string is empty, the only option is to# insert all characters of second string into firstifm==0:returnn# If second string is empty, the only option is to# remove all characters of first stringifn==0:returnm# If last characters of two strings are same, nothing# much to do. Get the count for# remaining strings.ifs1[m-1]==s2[n-1]:returneditDistRec(s1,s2,m-1,n-1)# If last characters are not same, consider all three# operations on last character of first string,# recursively compute minimum cost for all three# operations and take minimum of three values.return1+min(editDistRec(s1,s2,m,n-1),editDistRec(s1,s2,m-1,n),editDistRec(s1,s2,m-1,n-1))# Wrapper function to initiate# the recursive calculationdefeditDistance(s1,s2):returneditDistRec(s1,s2,len(s1),len(s2))if__name__=="__main__":s1="abcd"s2="bcfe"print(editDistance(s1,s2))
C#
// A Naive recursive C# program to find minimum number// of operations to convert s1 to s2.usingSystem;classGfG{// Recursive function to find number of operations // needed to convert s1 into s2.staticinteditDistRec(strings1,strings2,intm,intn){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]==s2[n-1])returneditDistRec(s1,s2,m-1,n-1);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.return1+Math.Min(Math.Min(editDistRec(s1,s2,m,n-1),editDistRec(s1,s2,m-1,n)),editDistRec(s1,s2,m-1,n-1));}// Wrapper function to initiate the recursive calculationstaticinteditDistance(strings1,strings2){returneditDistRec(s1,s2,s1.Length,s2.Length);}staticvoidMain(string[]args){strings1="abcd";strings2="bcfe";Console.WriteLine(editDistance(s1,s2));}}
JavaScript
// A Naive recursive JavaScript program to find minimum number// of operations to convert s1 to s2.// Recursive function to find number of operations // needed to convert s1 into s2.functioneditDistRec(s1,s2,m,n){// If first string is empty, the only option is to// insert all characters of second string into firstif(m===0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n===0)returnm;// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]===s2[n-1])returneditDistRec(s1,s2,m-1,n-1);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.return1+Math.min(editDistRec(s1,s2,m,n-1),editDistRec(s1,s2,m-1,n),editDistRec(s1,s2,m-1,n-1));}// Wrapper function to initiate the recursive calculationfunctioneditDistance(s1,s2){returneditDistRec(s1,s2,s1.length,s2.length);}// Driver Codeconsts1="abcd";consts2="bcfe";console.log(editDistance(s1,s2));
Output
3
[Better Approach - 1] Memoization - O(m * n) Time and O(m * n) Space
In the above recursive approach, there are several overlapping subproblems: editDist(m-1, n-1) is called Three times editDist(m-1, n-2) is called Two times editDist(m-2, n-1) is called Two times. And so on...
So, we can use Memoization to store the result of each subproblems to avoid recalculating the result again and again.
Below is the illustration of overlapping subproblems during the recursion.
C++
// C++ program to find minimum number// of operations to convert s1 to s2#include<iostream>#include<algorithm>#include<vector>usingnamespacestd;// Recursive function to find number of operations // needed to convert s1 into s2.inteditDistRec(string&s1,string&s2,intm,intn,vector<vector<int>>&memo){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If value is memoizedif(memo[m][n]!=-1)returnmemo[m][n];// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]==s2[n-1])returnmemo[m][n]=editDistRec(s1,s2,m-1,n-1,memo);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.returnmemo[m][n]=1+min({editDistRec(s1,s2,m,n-1,memo),editDistRec(s1,s2,m-1,n,memo),editDistRec(s1,s2,m-1,n-1,memo)});}// Wrapper function to initiate the recursive calculationinteditDistance(string&s1,string&s2){intm=s1.length(),n=s2.length();vector<vector<int>>memo(m+1,vector<int>(n+1,-1));returneditDistRec(s1,s2,m,n,memo);}intmain(){strings1="abcd";strings2="bcfe";cout<<editDistance(s1,s2);return0;}
Java
// Java program to find minimum number// of operations to convert s1 to s2importjava.util.Arrays;classGfG{// Recursive function to find number of operations // needed to convert s1 into s2.staticinteditDistRec(Strings1,Strings2,intm,intn,int[][]memo){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If value is memoizedif(memo[m][n]!=-1)returnmemo[m][n];// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1.charAt(m-1)==s2.charAt(n-1))returnmemo[m][n]=editDistRec(s1,s2,m-1,n-1,memo);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.returnmemo[m][n]=1+Math.min(editDistRec(s1,s2,m,n-1,memo),Math.min(editDistRec(s1,s2,m-1,n,memo),editDistRec(s1,s2,m-1,n-1,memo)));}// Wrapper function to initiate the recursive calculationstaticinteditDistance(Strings1,Strings2){intm=s1.length(),n=s2.length();int[][]memo=newint[m+1][n+1];for(int[]row:memo)Arrays.fill(row,-1);returneditDistRec(s1,s2,m,n,memo);}publicstaticvoidmain(String[]args){Strings1="abcd";Strings2="bcfe";System.out.println(editDistance(s1,s2));}}
Python
# Python program to find minimum number# of operations to convert s1 to s2# Recursive function to find number of operations # needed to convert s1 into s2.defeditDistRec(s1,s2,m,n,memo):# If first string is empty, the only option is to# insert all characters of second string into firstifm==0:returnn# If second string is empty, the only option is to# remove all characters of first stringifn==0:returnm# If value is memoizedifmemo[m][n]!=-1:returnmemo[m][n]# If last characters of two strings are same, nothing# much to do. Get the count for# remaining strings.ifs1[m-1]==s2[n-1]:memo[m][n]=editDistRec(s1,s2,m-1,n-1,memo)returnmemo[m][n]# If last characters are not same, consider all three# operations on last character of first string,# recursively compute minimum cost for all three# operations and take minimum of three values.memo[m][n]=1+min(editDistRec(s1,s2,m,n-1,memo),editDistRec(s1,s2,m-1,n,memo),editDistRec(s1,s2,m-1,n-1,memo))returnmemo[m][n]# Wrapper function to initiate the recursive calculationdefeditDistance(s1,s2):m,n=len(s1),len(s2)memo=[[-1for_inrange(n+1)]for_inrange(m+1)]returneditDistRec(s1,s2,m,n,memo)if__name__=="__main__":s1="abcd"s2="bcfe"print(editDistance(s1,s2))
C#
// C# program to find minimum number// of operations to convert s1 to s2usingSystem;classGfG{// Recursive function to find number of operations // needed to convert s1 into s2.staticinteditDistRec(strings1,strings2,intm,intn,int[,]memo){// If first string is empty, the only option is to// insert all characters of second string into firstif(m==0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n==0)returnm;// If value is memoizedif(memo[m,n]!=-1)returnmemo[m,n];// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]==s2[n-1])returnmemo[m,n]=editDistRec(s1,s2,m-1,n-1,memo);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.returnmemo[m,n]=1+Math.Min(editDistRec(s1,s2,m,n-1,memo),Math.Min(editDistRec(s1,s2,m-1,n,memo),editDistRec(s1,s2,m-1,n-1,memo)));}// Wrapper function to initiate the recursive calculationstaticinteditDistance(strings1,strings2){intm=s1.Length,n=s2.Length;int[,]memo=newint[m+1,n+1];for(inti=0;i<=m;i++)for(intj=0;j<=n;j++)memo[i,j]=-1;returneditDistRec(s1,s2,m,n,memo);}staticvoidMain(string[]args){strings1="abcd";strings2="bcfe";Console.WriteLine(editDistance(s1,s2));}}
JavaScript
// JavaScript program to find minimum number// of operations to convert s1 to s2// Recursive function to find number of operations // needed to convert s1 into s2.functioneditDistRec(s1,s2,m,n,memo){// If first string is empty, the only option is to// insert all characters of second string into firstif(m===0)returnn;// If second string is empty, the only option is to// remove all characters of first stringif(n===0)returnm;// If value is memoizedif(memo[m][n]!==-1)returnmemo[m][n];// If last characters of two strings are same, nothing// much to do. Get the count for// remaining strings.if(s1[m-1]===s2[n-1])returnmemo[m][n]=editDistRec(s1,s2,m-1,n-1,memo);// If last characters are not same, consider all three// operations on last character of first string,// recursively compute minimum cost for all three// operations and take minimum of three values.returnmemo[m][n]=1+Math.min(editDistRec(s1,s2,m,n-1,memo),Math.min(editDistRec(s1,s2,m-1,n,memo),editDistRec(s1,s2,m-1,n-1,memo)));}// Wrapper function to initiate the recursive calculationfunctioneditDistance(s1,s2){constm=s1.length,n=s2.length;constmemo=Array.from({length:m+1},()=>Array(n+1).fill(-1));returneditDistRec(s1,s2,m,n,memo);}// Driver Codeconsts1="abcd";consts2="bcfe";console.log(editDistance(s1,s2));
Output
3
[Better Approach - 2] Tabulation - O(m * n) Time and O(m * n) Space
Use a table to store solutions of subproblems to avoiding recalculate the same subproblems multiple times. By doing this, if same subproblems repeated during, we retrieve the solutions from the table itself.
Below are the steps to convert the recursive approach to Bottom up approach:
1. Dimensions of Table: The state of smaller sub-problems depends on the input parameters m and n because at least one of them will decrease after each recursive call. So we need to construct a 2D table dp[][] to store the solution of the sub-problems.
2. Size of Table: The range of parameters goes from 0 to m and 0 to n. So we choose dimensions as (m + 1)*(n + 1).
3. Filling the table: It consist of two stages, table initialisation and building the solution from the smaller subproblems:
Initialization: Before building the solution, we need to initialise the table with the known values i.e. base case. Here m = 0 and n = 0 is the situation of the base case, so we initialise first-column dp[i][0] with i and first-row dp[0][j] with j.
Building from smaller subproblems: We can easily define the iterative structure by using the recursive structure of the above recursive solution.
4. Returning final solution: After filling the table iteratively, our final solution gets stored at the bottom right corner of the 2-D table i.e. we return dp[m][n] as an output.
C++
// C++ program to find minimum number// of operations to convert s1 to s2#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;inteditDistance(string&s1,string&s2){intm=s1.length();intn=s2.length();// Create a table to store results of subproblemsvector<vector<int>>dp(m+1,vector<int>(n+1));// Fill the known entries in dp[][]// If one string is empty, then answer // is length of the other stringfor(inti=0;i<=m;i++)dp[i][0]=i;for(intj=0;j<=n;j++)dp[0][j]=j;// Fill the rest of dp[][]for(inti=1;i<=m;i++){for(intj=1;j<=n;j++){if(s1[i-1]==s2[j-1])dp[i][j]=dp[i-1][j-1];elsedp[i][j]=1+min({dp[i][j-1],dp[i-1][j],dp[i-1][j-1]});}}returndp[m][n];}intmain(){strings1="abcd";strings2="bcfe";cout<<editDistance(s1,s2);return0;}
Java
// Java program to find minimum number// of operations to convert s1 to s2importjava.util.*;classGfG{// Function to find the minimum number// of operations to convert s1 to s2staticinteditDistance(Strings1,Strings2){intm=s1.length();intn=s2.length();// Create a table to store results of subproblemsint[][]dp=newint[m+1][n+1];// Fill the known entries in dp[][]// If one string is empty, then answer// is length of the other stringfor(inti=0;i<=m;i++)dp[i][0]=i;for(intj=0;j<=n;j++)dp[0][j]=j;// Fill the rest of dp[][]for(inti=1;i<=m;i++){for(intj=1;j<=n;j++){if(s1.charAt(i-1)==s2.charAt(j-1))dp[i][j]=dp[i-1][j-1];elsedp[i][j]=1+Math.min(Math.min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1]);}}returndp[m][n];}publicstaticvoidmain(String[]args){Strings1="abcd";Strings2="bcfe";System.out.println(editDistance(s1,s2));}}
Python
# Python program to find minimum number# of operations to convert s1 to s2# Function to find the minimum number # of operations to convert s1 to s2defeditDistance(s1,s2):m=len(s1)n=len(s2)# Create a table to store results of subproblemsdp=[[0]*(n+1)for_inrange(m+1)]# Fill the known entries in dp[][]# If one string is empty, then answer# is length of the other stringforiinrange(m+1):dp[i][0]=iforjinrange(n+1):dp[0][j]=j# Fill the rest of dp[][]foriinrange(1,m+1):forjinrange(1,n+1):ifs1[i-1]==s2[j-1]:dp[i][j]=dp[i-1][j-1]else:dp[i][j]=1+min(dp[i][j-1],dp[i-1][j],dp[i-1][j-1])returndp[m][n]if__name__=="__main__":s1="abcd"s2="bcfe"print(editDistance(s1,s2))
C#
// C# program to find minimum number// of operations to convert s1 to s2usingSystem;classGfG{// Function to find the minimum number // of operations to convert s1 to s2staticinteditDistance(strings1,strings2){intm=s1.Length;intn=s2.Length;// Create a table to store results of subproblemsint[,]dp=newint[m+1,n+1];// Fill the known entries in dp[][]// If one string is empty, then answer// is length of the other stringfor(inti=0;i<=m;i++)dp[i,0]=i;for(intj=0;j<=n;j++)dp[0,j]=j;// Fill the rest of dp[][]for(inti=1;i<=m;i++){for(intj=1;j<=n;j++){if(s1[i-1]==s2[j-1])dp[i,j]=dp[i-1,j-1];elsedp[i,j]=1+Math.Min(Math.Min(dp[i,j-1],dp[i-1,j]),dp[i-1,j-1]);}}returndp[m,n];}staticvoidMain(string[]args){strings1="abcd";strings2="bcfe";Console.WriteLine(editDistance(s1,s2));}}
JavaScript
// JavaScript program to find minimum number// of operations to convert s1 to s2// Function to find the minimum number // of operations to convert s1 to s2functioneditDistance(s1,s2){letm=s1.length;letn=s2.length;// Create a table to store results of subproblemsletdp=Array.from({length:m+1},()=>Array(n+1).fill(0));// Fill the known entries in dp[][]// If one string is empty, then answer// is length of the other stringfor(leti=0;i<=m;i++)dp[i][0]=i;for(letj=0;j<=n;j++)dp[0][j]=j;// Fill the rest of dp[][]for(leti=1;i<=m;i++){for(letj=1;j<=n;j++){if(s1[i-1]===s2[j-1])dp[i][j]=dp[i-1][j-1];elsedp[i][j]=1+Math.min(dp[i][j-1],dp[i-1][j],dp[i-1][j-1]);}}returndp[m][n];}// Driver Codelets1="abcd";lets2="bcfe";console.log(editDistance(s1,s2));
Output
3
[Expected Approach] Space Optimized Approach – O(m * n) Time and O(n) Space
To fill a row in DP array we require only one row i.e. the upper row. For example, if we are filling the row where i=10 in DP array then we require only values of 9th row.
The idea is to first optimize the space by using two 1-D arrays, prev[] and curr[], instead of a 2-D DP array, since each row depends only on the previous row and the current row.
In the above two array approach, curr[j] is calculated using three values: prev[j-1] (diagonal), prev[j] (above), and curr[j-1] (left). We further optimize space by storing the diagonal value in a variable prev and using the single array curr[] to store both the previous and current row values.
Thus, by saving each cell's old value before overwriting it, we can compute the DP table using just one 1-D array.
C++
// C++ program to find minimum number// of operations to convert s1 to s2#include<bits/stdc++.h>usingnamespacestd;inteditDistance(string&s1,string&s2){intm=s1.size();intn=s2.size();// Stores dp[i-1][j-1]intprev;vector<int>curr(n+1,0);for(intj=0;j<=n;j++)curr[j]=j;for(inti=1;i<=m;i++){prev=curr[0];curr[0]=i;for(intj=1;j<=n;j++){inttemp=curr[j];if(s1[i-1]==s2[j-1])curr[j]=prev;elsecurr[j]=1+min({curr[j-1],prev,curr[j]});prev=temp;}}returncurr[n];}intmain(){strings1="abcd";strings2="bcfe";cout<<editDistance(s1,s2);return0;}
Java
// Java program to find minimum number// of operations to convert s1 to s2importjava.util.*;classGfG{// Function to find the minimum number // of operations to convert s1 to s2staticinteditDistance(Strings1,Strings2){intm=s1.length();intn=s2.length();// Stores dp[i-1][j-1]intprev;int[]curr=newint[n+1];for(intj=0;j<=n;j++)curr[j]=j;for(inti=1;i<=m;i++){prev=curr[0];curr[0]=i;for(intj=1;j<=n;j++){inttemp=curr[j];if(s1.charAt(i-1)==s2.charAt(j-1))curr[j]=prev;elsecurr[j]=1+Math.min(Math.min(curr[j-1],prev),curr[j]);prev=temp;}}returncurr[n];}publicstaticvoidmain(String[]args){Strings1="abcd";Strings2="bcfe";System.out.println(editDistance(s1,s2));}}
Python
# Python program to find minimum number# of operations to convert s1 to s2# Function to find the minimum number # of operations to convert s1 to s2defeditDistance(s1,s2):m=len(s1)n=len(s2)# Stores dp[i-1][j-1]prev=0curr=[0]*(n+1)forjinrange(n+1):curr[j]=jforiinrange(1,m+1):prev=curr[0]curr[0]=iforjinrange(1,n+1):temp=curr[j]ifs1[i-1]==s2[j-1]:curr[j]=prevelse:curr[j]=1+min(curr[j-1],prev,curr[j])prev=tempreturncurr[n]if__name__=="__main__":s1="abcd"s2="bcfe"print(editDistance(s1,s2))
C#
// C# program to find minimum number// of operations to convert s1 to s2usingSystem;classGfG{// Function to find the minimum number // of operations to convert s1 to s2staticinteditDistance(strings1,strings2){intm=s1.Length;intn=s2.Length;// Stores dp[i-1][j-1]intprev;int[]curr=newint[n+1];for(intj=0;j<=n;j++)curr[j]=j;for(inti=1;i<=m;i++){prev=curr[0];curr[0]=i;for(intj=1;j<=n;j++){inttemp=curr[j];if(s1[i-1]==s2[j-1])curr[j]=prev;elsecurr[j]=1+Math.Min(Math.Min(curr[j-1],prev),curr[j]);prev=temp;}}returncurr[n];}staticvoidMain(string[]args){strings1="abcd";strings2="bcfe";Console.WriteLine(editDistance(s1,s2));}}
JavaScript
// JavaScript program to find minimum number// of operations to convert s1 to s2// Function to find the minimum number // of operations to convert s1 to s2functioneditDistance(s1,s2){letm=s1.length;letn=s2.length;// Stores dp[i-1][j-1]letprev=0;letcurr=newArray(n+1).fill(0);for(letj=0;j<=n;j++)curr[j]=j;for(leti=1;i<=m;i++){prev=curr[0];curr[0]=i;for(letj=1;j<=n;j++){lettemp=curr[j];if(s1[i-1]===s2[j-1])curr[j]=prev;elsecurr[j]=1+Math.min(curr[j-1],prev,curr[j]);prev=temp;}}returncurr[n];}// Driver Codelets1="abcd";lets2="bcfe";console.log(editDistance(s1,s2));
Output
3
Real-World Applications of Edit Distance -
Spell Checking and Auto-Correction
DNA Sequence Alignment
Plagiarism Detection
Natural Language Processing
Version Control Systems
String Matching
Edit Distance and Longest Common Subsequence -
If we do not consider the replace operation, then edit distance problem is same as the Longest Common Subsequence (LCS) problem. With only insert and delete operations allowed, the edit distance between two strings is ( m + n - 2* LCS)