Substrings starting with vowel and ending with consonants and vice versa | Returns true if ch is vowel ; Function to check consonant ; in case of empty string , we can 't fullfill the  required condition, hence we return ans as 0. ; co [ i ] is going to store counts of consonants from str [ len - 1 ] to str [ i ] . vo [ i ] is going to store counts of vowels from str [ len - 1 ] to str [ i ] . ; Counting consonants and vowels from end of string . ; Now we traverse string from beginning ; If vowel , then count of substrings starting with str [ i ] is equal to count of consonants after it . ; If consonant , then count of substrings starting with str [ i ] is equal to count of vowels after it . ; Driver Code
def isVowel ( ch ) : NEW_LINE INDENT return ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) NEW_LINE DEDENT def isCons ( ch ) : NEW_LINE INDENT return ( ch != ' a ' and ch != ' e ' and ch != ' i ' and ch != ' o ' and ch != ' u ' ) NEW_LINE DEDENT def countSpecial ( str ) : NEW_LINE INDENT lent = len ( str ) NEW_LINE if lent == 0 : NEW_LINE return 0 ; NEW_LINE co = [ ] NEW_LINE vo = [ ] NEW_LINE for i in range ( 0 , lent + 1 ) : NEW_LINE INDENT co . append ( 0 ) NEW_LINE DEDENT for i in range ( 0 , lent + 1 ) : NEW_LINE INDENT vo . append ( 0 ) NEW_LINE DEDENT if isCons ( str [ lent - 1 ] ) == 1 : NEW_LINE INDENT co [ lent - 1 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT vo [ lent - 1 ] = 1 NEW_LINE DEDENT for i in range ( lent - 2 , - 1 , - 1 ) : NEW_LINE INDENT if isCons ( str [ i ] ) == 1 : NEW_LINE INDENT co [ i ] = co [ i + 1 ] + 1 NEW_LINE vo [ i ] = vo [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT co [ i ] = co [ i + 1 ] NEW_LINE vo [ i ] = vo [ i + 1 ] + 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( lent ) : NEW_LINE INDENT if isVowel ( str [ i ] ) : NEW_LINE INDENT ans = ans + co [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + vo [ i + 1 ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT str = " adceba " NEW_LINE print ( countSpecial ( str ) ) NEW_LINE