Longest substring of vowels with no two adjacent alphabets same | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isvowel ( char c ) { return c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ; } int findMaxLen ( string & s ) { int maxLen = 0 ; int cur = 0 ; if ( isvowel ( s [ 0 ] ) ) cur = maxLen = 1 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( isvowel ( s [ i ] ) ) { if ( s [ i ] != s [ i - 1 ] ) cur += 1 ; else cur = 1 ; } else { cur = 0 ; } maxLen = max ( cur , maxLen ) ; } return maxLen ; } int main ( ) { string Str = " aeoibsddaeiouudb " ; cout << findMaxLen ( Str ) << endl ; return 0 ; }