Count of substrings consisting of even number of vowels | Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def countSubstrings ( s , n ) : NEW_LINE INDENT temp = [ 1 , 0 ] ; NEW_LINE result = 0 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += ( 1 if isVowel ( s [ i ] ) else 0 ) ; NEW_LINE sum %= 2 ; NEW_LINE temp [ sum ] += 1 ; NEW_LINE DEDENT result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) // 2 ) ; NEW_LINE result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) // 2 ) ; NEW_LINE print ( result ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE s = " abcde " ; NEW_LINE countSubstrings ( s , n ) ; NEW_LINE DEDENT