Length of the longest substring consisting only of vowels in non | Java program for the above approach ; Function to find length of the longest substring consisting only of vowels in non - increasing order ; Stores all vowels in decreasing order ; Stores current index of array ch [ ] ; Stores the result ; Stores the count of current substring ; Declare a HashSet to store the vowels ; Traverse the string , S ; If S [ i ] is equal to ch [ j ] ; Increment count by 1 ; Add S [ i ] in the mp ; If length of mp is 5 , update res ; Else if j + 1 is less than 5 and S [ i ] is equal to ch [ j + 1 ] ; Add the S [ i ] in the mp ; Increment count by 1 ; Increment j by 1 ; If length of mp is 5 , update res ; Clear the mp ; If S [ i ] is ' u ' ; Add S [ i ] in the mp ; Update j and assign 1 to count ; Else assign 0 to j and count ; Return the result ; Driver Code ; Given Input ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int count ( String S , int N ) { char ch [ ] = { ' u ' , ' o ' , ' i ' , ' e ' , ' a ' } ; int j = 0 ; int res = 0 ; int count = 0 ; HashSet < Character > mp = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( S . charAt ( i ) == ch [ j ] ) { ++ count ; mp . add ( S . charAt ( i ) ) ; if ( mp . size ( ) == 5 ) { res = Math . max ( res , count ) ; } } else if ( j + 1 < 5 && S . charAt ( i ) == ch [ j + 1 ] ) { mp . add ( S . charAt ( i ) ) ; ++ count ; j ++ ; if ( mp . size ( ) == 5 ) { res = Math . max ( res , count ) ; } } else { mp . clear ( ) ; if ( S . charAt ( i ) == ' u ' ) { mp . add ( ' u ' ) ; j = 0 ; count = 1 ; } else { j = 0 ; count = 0 ; } } } return res ; } public static void main ( String [ ] args ) { String S = " ueiaoaeiouuoiea " ; int N = S . length ( ) ; System . out . println ( count ( S , N ) ) ; } }