Count the number of vowels occurring in all the substrings of given string | Java implementation of the above approach ; Returns the total sum of occurrences of all vowels ; No . of occurrences of 0 th character in all the substrings ; No . of occurrences of ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver Code
import java . io . * ; import java . util . * ; public class Gfg { static int vowel_calc ( String s ) { int n = s . length ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) arr [ i ] = n ; else arr [ i ] = ( n - i ) + arr [ i - 1 ] - i ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) sum += arr [ i ] ; } return sum ; } public static void main ( String args [ ] ) { String s = " daceh " ; System . out . println ( vowel_calc ( s ) ) ; } }