Maximum score possible by removing substrings made up of single distinct character | C # program for the above approach ; Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isUnique ( string s ) { HashSet < char > set = new HashSet < char > ( ) ; foreach ( char c in s ) set . Add ( c ) ; return set . Count == 1 ; } static int maxScore ( string s , int [ ] a ) { int n = s . Length ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { string sub = s . Substring ( i , j + 1 - i ) ; if ( isUnique ( sub ) ) mx = Math . Max ( mx , a [ sub . Length - 1 ] + maxScore ( s . Substring ( 0 , i ) + s . Substring ( j + 1 ) , a ) ) ; } } return mx ; } static void Main ( ) { string s = "011" ; int [ ] a = { 1 , 3 , 1 } ; Console . Write ( maxScore ( s , a ) ) ; } }