Maximize length of subsequence consisting of single distinct character possible by K increments in a string | C # program for the above approach ; Function to find the maximum length of a subsequence of same characters after at most K increment operations ; Store the size of s ; Sort the given string sort ( S . begin ( ) , S . end ( ) ) ; convert input string to char array ; sort tempArray ; Stores the maximum length and the sum of the sliding window ; Traverse the string S ; Add the current character to the window ; Decrease the window size ; Update the value of sum ; Increment the value of start ; Update the maximum window size ; Print the resultant maximum length of the subsequence ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maxSubsequenceLen ( string s , int K ) { int N = s . Length ; int start = 0 , end = 0 ; char [ ] S = s . ToCharArray ( ) ; Array . Sort ( S ) ; int ans = Int32 . MinValue , sum = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum = sum + ( S [ end ] - ' a ' ) ; while ( sum + K < ( S [ end ] - ' a ' ) * ( end - start + 1 ) ) { sum = sum - ( S [ start ] - ' a ' ) ; start ++ ; } ans = Math . Max ( ans , end - start + 1 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { string S = " acscbcca " ; int K = 1 ; maxSubsequenceLen ( S , K ) ; } }