Queries to find the count of vowels in the substrings of the given string | Java 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
class GFG { static final int N = 2 ; static Boolean isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static void performQueries ( String str , int len , int queries [ ] [ ] , int q ) { int [ ] pre = new int [ len ] ; if ( isVowel ( str . charAt ( 0 ) ) ) pre [ 0 ] = 1 ; else pre [ 0 ] = 0 ; for ( int i = 1 ; i < len ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) pre [ i ] = 1 + pre [ i - 1 ] ; else pre [ i ] = pre [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i ] [ 0 ] == 0 ) { System . out . println ( pre [ queries [ i ] [ 1 ] ] ) ; } else { System . out . println ( ( pre [ queries [ i ] [ 1 ] ] - pre [ queries [ i ] [ 0 ] - 1 ] ) ) ; } } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; int queries [ ] [ ] = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = queries . length ; performQueries ( str , len , queries , q ) ; } }