Maximum length subsequence possible of the form R ^ N K ^ N | C # program to find the maximum length of a substring of form R ^ nK ^ 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
using System ; class GFG { static int find ( String s ) { int max = 0 , i , j = 0 , countk = 0 , countr = 0 ; int [ , ] table = new int [ s . Length , 2 ] ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' R ' ) countr ++ ; else table [ ( j ++ ) , 0 ] = countr ; } j -- ; for ( i = s . Length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' K ' ) { 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 ; } static public void Main ( String [ ] srgs ) { String s = " RKRRRKKRRKKKKRR " ; int n = find ( s ) ; Console . WriteLine ( n ) ; } }