Minimum operations to transform given string to another by moving characters to front or end | Python3 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
dp = [ [ 0 ] * 1010 ] * 1010 NEW_LINE def solve ( s , t ) : NEW_LINE INDENT n = len ( s ) NEW_LINE r = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE if ( i > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE DEDENT if ( s [ i ] == t [ j ] ) : NEW_LINE INDENT ans = 1 NEW_LINE if ( i > 0 and j > 0 ) : NEW_LINE INDENT ans = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , ans ) NEW_LINE r = max ( r , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return ( n - r ) NEW_LINE DEDENT s = " abcde " NEW_LINE t = " edacb " NEW_LINE print ( solve ( s , t ) ) NEW_LINE