Count characters to be shifted from the start or end of a string to obtain another string | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int minCost ( string A , string B ) { int n = A . Length ; int i = 0 ; int maxlen = 0 ; while ( i < n ) { int length = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( A [ i ] == B [ j ] ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = Math . Max ( maxlen , length ) ; } return n - maxlen ; } public static void Main ( ) { string A = " edacb " ; string B = " abcde " ; Console . WriteLine ( minCost ( A , B ) ) ; } }