Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | C # program for the above approach ; Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void maximumLengthSubsequence ( String str ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ch ) ) mp [ ch ] = mp [ ch ] + 1 ; else mp . Add ( ch , 1 ) ; } int ans = 0 ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ( char ) ( ch - 1 ) ) ) { int curr_max = mp [ ch ] + mp [ ( char ) ( ch - 1 ) ] ; ans = Math . Max ( ans , curr_max ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = " acbbebcg " ; maximumLengthSubsequence ( S ) ; } }