Length of the longest substring that contains even number of vowels | 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 ; 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
def longestSubstring ( s ) : NEW_LINE INDENT indexes = { } NEW_LINE chars = { } NEW_LINE chars [ ' a ' ] = 0 NEW_LINE chars [ ' e ' ] = 1 NEW_LINE chars [ ' i ' ] = 2 NEW_LINE chars [ ' o ' ] = 3 NEW_LINE chars [ ' u ' ] = 4 NEW_LINE evenOdd = "00000" NEW_LINE evenOdd = [ i for i in evenOdd ] NEW_LINE indexes [ " " . join ( evenOdd ) ] = - 1 NEW_LINE length = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT c = s [ i ] NEW_LINE if ( c in chars ) : NEW_LINE INDENT evenOdd [ chars [ it ] ] = '1' if evenOdd [ chars [ it ] ] else '0' NEW_LINE DEDENT if ( " " . join ( evenOdd ) not in indexes ) : NEW_LINE INDENT indexes [ " " . join ( evenOdd ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT length = max ( length , i - indexes [ " " . join ( evenOdd ) ] ) NEW_LINE DEDENT DEDENT print ( length ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " bcbcbc " NEW_LINE longestSubstring ( S ) NEW_LINE DEDENT