Length of the longest substring that contains even number of vowels | C ++ program for the above approach ; Function to find the length of the longest substring having even number of vowels ; Create two hashmaps ; Keep the track of frequencies of the vowels ; Stores the maximum length ; Traverse the given string S ; Find character in the map ; If it is a vowel , then update the frequency ; Find the index of occurence of the string evenOdd in map ; Update the maximum length ; Print the maximum length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubstring ( string s ) { unordered_map < string , int > indexes ; unordered_map < char , int > chars ( { { ' a ' , 0 } , { ' e ' , 1 } , { ' i ' , 2 } , { ' o ' , 3 } , { ' u ' , 4 } } ) ; string evenOdd = "00000" ; indexes [ evenOdd ] = -1 ; int length = 0 ; for ( int i = 0 ; i < s . size ( ) ; ++ i ) { char c = s [ i ] ; auto it = chars . find ( c ) ; if ( it != chars . end ( ) ) { evenOdd [ it -> second ] = evenOdd [ it -> second ] == '0' ? '1' : '0' ; } auto lastIndex = indexes . find ( evenOdd ) ; if ( lastIndex == indexes . end ( ) ) { indexes [ evenOdd ] = i ; } else { length = max ( length , i - lastIndex -> second ) ; } } cout << length ; } int main ( ) { string S = " bcbcbc " ; longestSubstring ( S ) ; return 0 ; }