Minimum operations to transform given string to another by moving characters to front or end | Java 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 ; Driver Code ; Given String s , t ; Function Call
class GFG { static int [ ] [ ] dp = new int [ 1010 ] [ 1010 ] ; static int solve ( String s , String t ) { int n = s . length ( ) ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int 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 . charAt ( i ) == t . charAt ( j ) ) { int 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 ) ; } public static void main ( String [ ] args ) { String s = " abcde " ; String t = " edacb " ; System . out . print ( solve ( s , t ) ) ; } }