Minimize removals to remove another string as a subsequence of a given string | Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If str [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Input ; Function call to get minimum number of character removals
function printMinimumRemovals ( str , X ) { let N = str . length ; let M = X . length ; let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = new Array ( M ) ; for ( let j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( let j = 0 ; j < M ; j ++ ) { if ( str [ 0 ] == X [ j ] ) { dp [ 0 ] [ j ] = 1 ; } } for ( let i = 1 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( str [ i ] == X [ j ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } document . write ( dp [ N - 1 ] [ M - 1 ] ) ; } let str = " " ; let X = " " ; printMinimumRemovals ( str , X ) ;