# Edit distance and LCS (Longest Common Subsequence)

In standard Edit Distance where we are allowed 3 operations, insert, delete and replace. Consider a variation of edit distance where we are allowed only two operations insert and delete, find edit distance in this variation.
Examples:

```Input : str1 = "cat", st2 = "cut"
Output : 2
We are allowed to insert and delete. We delete 'a'
from "cat" and insert "u" to make it "cut".

Input : str1 = "acb", st2 = "ab"
Output : 1
We can convert "acb" to "ab" by removing 'c'.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

One solution is to simply modify Edit Distance Solution by making two recursive call instead of three. An interesting solution is based on LCS.
1) Find LCS of two strings. Let length of LCS be x.
2) Let length of first string be m and length of second string be n. Our result is (m – x) + (n – x). We basically need to do (m – x) delete operations and (n – x) insert operations.

## C++

 `// CPP program to find Edit Distance (when only two ` `// operations are allowed, insert and delete) using LCS. ` `#include ` `using` `namespace` `std; ` ` `  `int` `editDistanceWith2Ops(string &X, string &Y) ` `{ ` `    ``// Find LCS ` `    ``int` `m = X.length(), n = Y.length();  ` `    ``int` `L[m+1][n+1];  ` `    ``for` `(``int` `i=0; i<=m; i++) ` `    ``{ ` `        ``for` `(``int` `j=0; j<=n; j++) ` `        ``{ ` `            ``if` `(i == 0 || j == 0) ` `                ``L[i][j] = 0; ` `            ``else` `if` `(X[i-1] == Y[j-1]) ` `                ``L[i][j] = L[i-1][j-1] + 1; ` `            ``else` `                ``L[i][j] = max(L[i-1][j], L[i][j-1]); ` `        ``} ` `    ``}     ` `    ``int` `lcs  = L[m][n]; ` ` `  `    ``// Edit distance is delete operations +  ` `    ``// insert operations. ` `    ``return` `(m - lcs) + (n - lcs); ` `} ` ` `  `/* Driver program to test above function */` `int` `main() ` `{ ` `    ``string X = ``"abc"``, Y = ``"acd"``; ` `    ``cout << editDistanceWith2Ops(X, Y); ` `    ``return` `0; ` `} `

## Java

 `//Java program to find Edit Distance (when only two  ` `// operations are allowed, insert and delete) using LCS. ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `editDistanceWith2Ops(String X, String Y) { ` `        ``// Find LCS  ` `        ``int` `m = X.length(), n = Y.length(); ` `        ``int` `L[][] = ``new` `int``[m + ``1``][n + ``1``]; ` `        ``for` `(``int` `i = ``0``; i <= m; i++) { ` `            ``for` `(``int` `j = ``0``; j <= n; j++) { ` `                ``if` `(i == ``0` `|| j == ``0``) { ` `                    ``L[i][j] = ``0``; ` `                ``} ``else` `if` `(X.charAt(i - ``1``) == Y.charAt(j - ``1``)) { ` `                    ``L[i][j] = L[i - ``1``][j - ``1``] + ``1``; ` `                ``} ``else` `{ ` `                    ``L[i][j] = Math.max(L[i - ``1``][j], L[i][j - ``1``]); ` `                ``} ` `            ``} ` `        ``} ` `        ``int` `lcs = L[m][n]; ` ` `  `        ``// Edit distance is delete operations +  ` `        ``// insert operations.  ` `        ``return` `(m - lcs) + (n - lcs); ` `    ``} ` ` `  `    ``/* Driver program to test above function */` `    ``public` `static` `void` `main(String[] args) { ` `        ``String X = ``"abc"``, Y = ``"acd"``; ` `        ``System.out.println(editDistanceWith2Ops(X, Y)); ` ` `  `    ``} ` `} ` `/* This Java code is contributed by 29AjayKumar*/`

## Python 3

 `# Python 3 program to find Edit Distance  ` `# (when only two operations are allowed, ` `# insert and delete) using LCS. ` ` `  `def` `editDistanceWith2Ops(X, Y): ` ` `  `    ``# Find LCS ` `    ``m ``=` `len``(X)  ` `    ``n ``=` `len``(Y) ` `    ``L ``=` `[[``0` `for` `x ``in` `range``(m ``+` `1``)]  ` `            ``for` `y ``in` `range``(n ``+` `1``)]  ` `    ``for` `i ``in` `range``(m ``+` `1``): ` `        ``for` `j ``in` `range``(n ``+` `1``): ` `            ``if` `(i ``=``=` `0` `or` `j ``=``=` `0``): ` `                ``L[i][j] ``=` `0` `            ``elif` `(X[i ``-` `1``] ``=``=` `Y[j ``-` `1``]): ` `                ``L[i][j] ``=` `L[i ``-` `1``][j ``-` `1``] ``+` `1` `            ``else``: ` `                ``L[i][j] ``=` `max``(L[i ``-` `1``][j], ` `                              ``L[i][j ``-` `1``]) ` ` `  `    ``lcs ``=` `L[m][n] ` ` `  `    ``# Edit distance is delete operations +  ` `    ``# insert operations. ` `    ``return` `(m ``-` `lcs) ``+` `(n ``-` `lcs) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``X ``=` `"abc"` `    ``Y ``=` `"acd"` `    ``print``(editDistanceWith2Ops(X, Y)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to find Edit Distance  ` `// (when only two operations are  ` `// allowed, insert and delete) using LCS.  ` `using` `System; ` ` `  `class` `GFG  ` `{  ` ` `  `static` `int` `editDistanceWith2Ops(String X,  ` `                                ``String Y)  ` `{  ` `    ``// Find LCS  ` `    ``int` `m = X.Length, n = Y.Length;  ` `    ``int` `[ , ]L = ``new` `int``[m + 1 , n + 1];  ` `    ``for` `(``int` `i = 0; i <= m; i++) ` `    ``{  ` `        ``for` `(``int` `j = 0; j <= n; j++)  ` `        ``{  ` `            ``if` `(i == 0 || j == 0) ` `            ``{  ` `                ``L[i , j] = 0;  ` `            ``}  ` `            ``else` `if` `(X[i - 1] == Y[j - 1])  ` `            ``{  ` `                ``L[i , j] = L[i - 1 , j - 1] + 1;  ` `            ``}  ` `            ``else`  `            ``{  ` `                ``L[i , j] = Math.Max(L[i - 1 , j],  ` `                                    ``L[i , j - 1]);  ` `            ``}  ` `        ``}  ` `    ``}  ` `    ``int` `lcs = L[m , n];  ` ` `  `    ``// Edit distance is delete operations +  ` `    ``// insert operations.  ` `    ``return` `(m - lcs) + (n - lcs);  ` `}  ` ` `  `// Driver Code ` `public` `static` `void` `Main()  ` `{  ` `    ``String X = ``"abc"``, Y = ``"acd"``;  ` `    ``Console.Write(editDistanceWith2Ops(X, Y));  ` `}  ` `}  ` ` `  `// This code is contributed  ` `// by 29AjayKumar `

## PHP

 ` `

Output:

```2
```

Time Complexity : O(m * n)
Auxiliary Space : O(m * n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.