text: Longest Substring having equal count of Vowels and Consonants | Java program to implement the above approach ; Function to return the length of the longest subString having equal number of vowel and consonant ; Generate the array ; Initialize variable to store result ; Stores the sum of subarray ; Map to store indices of the sum ; Loop through the array ; If sum is 0 ; Count of vowels and consonants are equal ; Update the maximum length of subString in HashMap ; Store the index of the sum ; Return the maximum length of required subString ; Driver Code
code: import java . util . * ; class GFG { static int maxsubStringLength ( char [ ] S , int N ) { int arr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; int maxLen = 0 ; int curr_sum = 0 ; HashMap < Integer , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum += arr [ i ] ; if ( curr_sum == 0 ) maxLen = Math . max ( maxLen , i + 1 ) ; if ( hash . containsKey ( curr_sum ) ) maxLen = Math . max ( maxLen , i - hash . get ( curr_sum ) ) ; else hash . put ( curr_sum , i ) ; } return maxLen ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int n = S . length ( ) ; System . out . print ( maxsubStringLength ( S . toCharArray ( ) , n ) ) ; } }