Maximum length subsequence possible of the form R ^ N K ^ N | function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code
function find ( s ) { let max = 0 , i , j = 0 , countk = 0 , countr = 0 ; let table = new Array ( s . length ) ; for ( let i = 0 ; i < s . length ; i ++ ) { table [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) countr ++ ; else table [ j ++ ] [ 0 ] = countr ; } j -- ; for ( i = s . length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { countk ++ ; table [ j -- ] [ 1 ] = countk ; } if ( Math . min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) > max ) max = Math . min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) ; } return max ; } let s = " " ; let n = find ( s ) ; document . write ( n ) ;