Longest substring of vowels with no two adjacent alphabets same | Java implementation of the above approach ; Function to check a character is vowel or not ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; Stores length of current valid subString ; If curr and prev character are not same , include it ; If same as prev one , start new subString from here ; Store max in maxLen ; Driver code
public class GFG { static boolean isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static int findMaxLen ( String s ) { int maxLen = 0 ; int cur ; if ( isVowel ( s . charAt ( 0 ) ) ) maxLen = 1 ; cur = maxLen ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) cur += 1 ; else cur = 1 ; } else { cur = 0 ; } maxLen = Math . max ( cur , maxLen ) ; } return maxLen ; } public static void main ( String [ ] args ) { String Str = " aeoibsddaeiouudb " ; System . out . println ( findMaxLen ( Str ) ) ; } }