Count characters to be shifted from the start or end of a string to obtain another string | Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Driver Code ; Given two strings A and B ; Function call
def minCost ( A , B ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE i = 0 ; NEW_LINE maxlen = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT length = 0 ; NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( A [ i ] == B [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE length += 1 ; NEW_LINE if ( i == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT maxlen = max ( maxlen , length ) ; NEW_LINE DEDENT return n - maxlen ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " edacb " ; NEW_LINE B = " abcde " ; NEW_LINE print ( minCost ( A , B ) ) ; NEW_LINE DEDENT