Edit distance and LCS ( Longest Common Subsequence ) | Javascript program to find Edit Distance ( when only twooperations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver program to test above function
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 = " " , Y = " " ; document . write ( editDistanceWith2Ops ( X , Y ) ) ;