Count the number of vowels occurring in all the substrings of given string | C ++ 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 the ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int vowel_calc ( string s ) { int n = s . length ( ) ; vector < int > arr ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) arr . push_back ( n ) ; else arr . push_back ( ( n - i ) + arr [ i - 1 ] - i ) ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) sum += arr [ i ] ; return sum ; } int main ( ) { string s = " daceh " ; cout << vowel_calc ( s ) << endl ; return 0 ; }