Longest subsequence with consecutive English alphabets | C # program for the above approach ; Function to find the length of subsequence starting with character ch ; Length of the string ; Stores the maximum length ; Traverse the given string ; If s [ i ] is required character ch ; Increment ans by 1 ; Increment character ch ; Return the current maximum length with character ch ; Function to find the maximum length of subsequence of consecutive characters ; Stores the maximum length of consecutive characters ; Update ans ; Return the maximum length of subsequence ; Driver Code ; Input ; Function Call
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static int findSubsequence ( String S , char ch ) { int N = S . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ch ) { ans ++ ; ch ++ ; } } return ans ; } static int findMaxSubsequence ( String S ) { int ans = 0 ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { ans = Math . max ( ans , findSubsequence ( S , ch ) ) ; } return ans ; } public static void main ( String [ ] args ) { String S = " abcabefghijk " ; System . out . print ( findMaxSubsequence ( S ) ) ; } }