
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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