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 ; Given two strings A and B ; Function call
function minCost ( A , B ) { var n = A . length ; var i = 0 ; var maxlen = 0 ; while ( i < n ) { var length = 0 ; for ( var j = 0 ; j < n ; ++ j ) { if ( A [ i ] == B [ j ] ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = Math . max ( maxlen , length ) ; } return n - maxlen ; } var A = " " ; var B = " " ; document . write ( minCost ( A , B ) ) ;