Count the number of vowels occurring in all the substrings of given string | Set 2
Last Updated :
28 Jan, 2022
Given a string str[] of length N of lowercase characters containing 0 or more vowels, the task is to find the count of vowels that occurred in all the substrings of the given string.
Examples:
Input: str = “abc”
Output: 3
The given string “abc” contains only one vowel = ‘a’
Substrings of “abc” are = {“a”, “b”, “c”, “ab”, “bc, “abc”}
Hence, the sum of occurrences of the vowel in these strings = 3.(‘a’ occurred 3 times)
Input: str = “daceh”
Output: 16
Prefix Sum Approach: The Naive Approach and the approach using prefix sum are discussed in the Set 1 of this article.
Efficient Approach: Suppose, given string is str, substring starts from position x, and ends at position y and if the vowel are at i-th position, where 0<=x<=i and i<=y<-N . for each vowel, it could be in the substring, that is (i+1) choices for x and (n-i) choices for y. So there are total (i+1)*(n-i) substrings containing str[i]. Take a constant “aeiou“. Follow the steps below to solve the problem:
- Initialize the variable res as 0 to store the answer.
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- If str[i] is a vowel then add the value of (I+1)*(N-i) to the variable res to count the total number of vowels possible.
- After performing the above steps, print the value of res as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long long countVowels(string str)
{
long res = 0, N = str.size();
for ( int i = 0; i < N; ++i)
if (string( "aeiou" ).find(str[i])
!= string::npos)
res += (i + 1) * (N - i);
return res;
}
int main()
{
string str = "daceh" ;
long long ans = countVowels(str);
cout << ans << endl;
return 0;
}
|
Java
class GFG{
static long countVowels(String str)
{
long res = 0 , N = str.length();
for ( int i = 0 ; i < N; ++i)
if ( new String( "aeiou" ).contains(String.valueOf(str.charAt(i))))
res += (i + 1 ) * (N - i);
return res;
}
public static void main(String[] args)
{
String str = "daceh" ;
long ans = countVowels(str);
System.out.print(ans + "\n" );
}
}
|
Python3
def countVowels( str ):
res = 0 ;
N = len ( str );
for i in range (N):
if (( str [i]) in ( "aeiou" )):
res + = (i + 1 ) * (N - i);
return res;
if __name__ = = '__main__' :
str = "daceh" ;
ans = countVowels( str );
print (ans);
|
C#
using System;
class GFG{
static long countVowels(String str)
{
long res = 0, N = str.Length;
for ( int i = 0; i < N; ++i)
if ( new String( "aeiou" ).Contains(str[i]))
res += (i + 1) * (N - i);
return res;
}
public static void Main()
{
String str = "daceh" ;
long ans = countVowels(str);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
function countVowels(str)
{
var res = 0, N = str.length;
for ( var i = 0; i < N; ++i)
if ( "aeiou" .includes(str[i]))
res += (i + 1) * (N - i);
return res;
}
var str = "daceh" ;
var ans = countVowels(str);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...