Longest equal substring with cost less than K | C # program to find the maximum length of equal subString within a given cost ; Function to find the maximum length ; Fill the prefix array with the difference of letters ; Update the maximum length ; Driver code
using System ; class GFG { static int solve ( string X , string Y , int N , int K ) { int [ ] count = new int [ N + 1 ] ; int sol = 0 ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count [ i ] = count [ i - 1 ] + Math . Abs ( X [ i - 1 ] - Y [ i - 1 ] ) ; } int j = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { while ( ( count [ i ] - count [ j ] ) > K ) { j ++ ; } sol = Math . Max ( sol , i - j ) ; } return sol ; } public static void Main ( ) { int N = 4 ; string X = " abcd " , Y = " bcde " ; int K = 3 ; Console . WriteLine ( solve ( X , Y , N , K ) + " STRNEWLINE " ) ; } }