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

C++Server Side ProgrammingProgramming

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
raja
Published on 05-Jan-2021 06:28:34
Advertisements