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'.
One solution is to simply modify the Edit Distance Solution by making two recursive calls instead of three. An interesting solution is based on LCS.
- Find LCS of two strings. Let the length of LCS be x.
- Let the length of the first string be m and the length of the 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.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int editDistanceWith2Ops(string &X, string &Y)
{
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];
return (m - lcs) + (n - lcs);
}
int main()
{
string X = "abc" , Y = "acd" ;
cout << editDistanceWith2Ops(X, Y);
return 0;
}
|
Java
class GFG {
static int editDistanceWith2Ops(String X, String Y) {
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];
return (m - lcs) + (n - lcs);
}
public static void main(String[] args) {
String X = "abc" , Y = "acd" ;
System.out.println(editDistanceWith2Ops(X, Y));
}
}
|
Python 3
def editDistanceWith2Ops(X, Y):
m = len (X)
n = len (Y)
L = [[ 0 for x in range (n + 1 )]
for y in range (m + 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]
return (m - lcs) + (n - lcs)
if __name__ = = "__main__" :
X = "abc"
Y = "acd"
print (editDistanceWith2Ops(X, Y))
|
C#
using System;
class GFG
{
static int editDistanceWith2Ops(String X,
String Y)
{
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];
return (m - lcs) + (n - lcs);
}
public static void Main()
{
String X = "abc" , Y = "acd" ;
Console.Write(editDistanceWith2Ops(X, Y));
}
}
|
PHP
<?php
function editDistanceWith2Ops( $X , $Y )
{
$m = strlen ( $X ); $n = strlen ( $Y );
$L [ $m + 1][ $n + 1];
for ( $i = 0; $i <= $m ; $i ++)
{
for ( $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]);
}
}
$lcs = $L [ $m ][ $n ];
return ( $m - $lcs ) + ( $n - $lcs );
}
$X = "abc" ; $Y = "acd" ;
echo editDistanceWith2Ops( $X , $Y );
?>
|
Javascript
<script>
function editDistanceWith2Ops(X,Y)
{
let m = X.length, n = Y.length;
let L = new Array(m + 1);
for (let i=0;i<L.length;i++)
{
L[i]= new Array(n+1);
for (let j=0;j<L[i].length;j++)
{
L[i][j]=0;
}
}
for (let i = 0; i <= m; i++) {
for (let 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]);
}
}
}
let lcs = L[m][n];
return (m - lcs) + (n - lcs);
}
let X = "abc" , Y = "acd" ;
document.write(editDistanceWith2Ops(X, Y));
</script>
|
Complexity Analysis:
- Time Complexity: O(m * n)
- Auxiliary Space: O(m * n)