Count the number of vowels occurring in all the substrings of given string in C++



Given a string str containing English alphabets. The goal is to find the number of vowels occurring in all the substrings of str. If string is β€œabcde” then substrings will be β€œa”, β€œb”, β€œc”, β€œd”, β€œe”, β€œab”, β€œbc”, β€œcd”, β€œde”, β€œabc”, β€œbcd”, β€œcde”, β€œabcd”, β€œbcde”, β€œabcde”. The count of vowels in these substrings is 10. (a and e)

For Example

Input

str = ”aloe”

Output

Count the number of vowels occurring in all the substrings of given string are:
14

Explanation

The substrings are:
β€œa”, β€œl”, β€œo”, β€œe”, β€œal”, β€œlo”, β€œoe”, β€œalo”, β€œloe”, β€œaloe”. Total vowels in these are: 14

Input

str=”http”

Output

Count the number of vowels occurring in all the substrings of given string are:
0

Explanation

The substrings are:
β€œh”, β€œt”, β€œt”, β€œp”, β€œht”, β€œtt”, β€œtp”, β€œhtt”, β€œttp”, β€œhttp”. Total vowels in these are: 0

Approach used in the below program is as follows βˆ’

In this approach we will create a vector vec, which stores counts of occurrences of ith character in all substrings in vec[i].

The 0th character occurs in n substrings where n is length of string str.

The ith character occurs in all substrings containing it ( nβˆ’i ) + number of substrings containing ith character as well as previous character ( arr[ iβˆ’1 ] ) βˆ’ number of substrings formed by previous characters only ( i ).

  • Take a string str as input.

  • Function substring_vowels_count(string str, int length) takes str with its length and returns the count the number of vowels occurring in all the substrings of a given string.

  • Take the initial count as 0.

  • Take an integer vector vec.

  • Traverse vec using a for loop from iβˆ’0 to i<length and populate it with counts of occurrences of ith position character in all substrings of str.

  • If i=0, for 0th character this count is length. Set vec[0]=length using push_back[length].

  • For all other characters set using push_back(temp_1 + temp_2) where temp_1=lengthβˆ’1 and temp_2=vec[iβˆ’1]βˆ’i.

  • Now traverse str again using for loop and for each str[i] as vowel, ( a , e, i, o, or u ). Add vec[i] to count.

  • At the end we will have a total number of occurrences of vowels in substrings in count.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int substring_vowels_count(string str, int length){
   int count = 0;
   vector<int> vec;
   for (int i = 0; i < length; i++){
      if (i == 0){
         vec.push_back(length);
      } else {
         int temp_1 = length βˆ’ i;
         int temp_2 = vec[i βˆ’ 1] βˆ’ i;
         vec.push_back(temp_1 + temp_2);
      }
   }
   for (int i = 0; i < length; i++){
      if(str[i] == 'a' || str[i] == 'i' || str[i] == 'e' || str[i] == 'o' || str[i] == 'u'){
         count = count + vec[i];
      }
   }
   return count;
}
int main(){
   string str = "honesty";
   int length = str.length();
   cout<<"Count the number of vowels occurring in all the substrings of given string are: "<<substring_vowels_count(str, length);
   return 0;
}

Output

If we run the above code it will generate the following output βˆ’

Count the number of vowels occurring in all the substrings of given string are: 28
Updated on: 2021-01-05T06:29:23+05:30

467 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements