Remove minimum characters from string to split it into three substrings under given constraints | Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Prlet the characters to be removed ; Given String ; Function call
function min_remove ( str ) { let N = str . length ; let prefix_a = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; let prefix_b = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; let prefix_c = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( let i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( ( str [ i - 1 ] == ' ' ) ? 1 : 0 ) ; } let maxi = Number . MIN_VALUE ; for ( let i = 0 ; i <= N ; i ++ ) { for ( let j = i ; j <= N ; j ++ ) { maxi = Math . max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } document . write ( ( N - maxi ) ) ; } let str = " " ; min_remove ( str ) ;