Queries to find the count of vowels in the substrings of the given string | Python 3 implementation of the approach ; Function that returns true if ch is a vowel ; pre [ i ] will store the count of vowels in the substring str [ 0. . . i ] ; Fill the pre [ ] array ; If current character is a vowel ; If its a consonant ; For every query ; Find the count of vowels for the current query ; Driver code
N = 2 NEW_LINE 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 performQueries ( str1 , len1 , queries , q ) : NEW_LINE INDENT pre = [ 0 for i in range ( len1 ) ] NEW_LINE if ( isVowel ( str1 [ 0 ] ) ) : NEW_LINE INDENT pre [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT pre [ 0 ] = 0 NEW_LINE DEDENT for i in range ( 0 , len1 , 1 ) : NEW_LINE INDENT if ( isVowel ( str1 [ i ] ) ) : NEW_LINE INDENT pre [ i ] = 1 + pre [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT if ( queries [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT print ( pre [ queries [ i ] [ 1 ] ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( pre [ queries [ i ] [ 1 ] ] - pre [ queries [ i ] [ 0 ] - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE len1 = len ( str1 ) NEW_LINE queries = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 1 , 9 ] ] NEW_LINE q = len ( queries ) NEW_LINE performQueries ( str1 , len1 , queries , q ) NEW_LINE DEDENT