Minimum operations to transform given string to another by moving characters to front or end | Javascript program for the above approach ; Function that finds the minimum number of steps to find the minimum characters must be moved to convert string s to t ; r = maximum value over all dp [ i ] [ j ] computed so far ; dp [ i ] [ j ] stores the longest contiguous suffix of T [ 0. . j ] that is subsequence of S [ 0. . i ] ; Update the maximum length ; Return the resulting length ; Given string s , t ; Function Call
var dp = Array . from ( Array ( 1010 ) , ( ) => Array ( 1010 ) ) ; function solve ( s , t ) { var n = s . length ; var r = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; if ( i > 0 ) { dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) ; } if ( s [ i ] == t [ j ] ) { var ans = 1 ; if ( i > 0 && j > 0 ) { ans = 1 + dp [ i - 1 ] [ j - 1 ] ; } dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , ans ) ; r = Math . max ( r , dp [ i ] [ j ] ) ; } } } return ( n - r ) ; } var s = " " ; var t = " " ; document . write ( solve ( s , t ) ) ;