# Count of substrings consisting only of vowels

Given a string S, the task is to count all substrings which contains only vowels.

Examples:

Input: S = “geeksforgeeks”
Output: 7
Explanation:
Substrings {“e”, “ee”, “e”, “o”, “e”, “ee”, “e”} consists only of vowels.

Input: S = “aecui”
Output: 6
Explanation:
Substrings {“a”, “ae”, “e”, “u”, “ui”, “i”} consists only of vowels.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach:
To simplest approach is to generate all substrings and check each of them if they contain only vowels or not.

Below is the implementation of the above approach:

## C++

 `// C++ program to Count all substrings ` `// in a string which contains only vowels ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if a ` `// character is vowel or not ` `bool` `isvowel(``char` `ch) ` `{ ` `    ``return` `(ch == ``'a'` `or ch == ``'e'` `            ``or ch == ``'i'` `or ch == ``'o'` `            ``or ch == ``'u'``); ` `} ` ` `  `// Function to check whether ` `// string contains only vowel ` `bool` `isvalid(string& s) ` `{ ` `    ``int` `n = s.length(); ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// Check if the character is ` `        ``// not vowel then invalid ` `        ``if` `(!isvowel(s[i])) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Function to Count all substrings ` `// in a string which contains ` `// only vowels ` `int` `CountTotal(string& s) ` `{ ` `    ``int` `ans = 0; ` ` `  `    ``int` `n = s.length(); ` ` `  `    ``// Generate all substring of s ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``string temp = ``""``; ` `        ``for` `(``int` `j = i; j < n; j++) { ` `            ``temp += s[j]; ` ` `  `            ``// If temp contains only vowels ` `            ``if` `(isvalid(temp)) ` `                ``// Increment the count ` `                ``ans += 1; ` `        ``} ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``string s = ``"aeoibsddaaeiouudb"``; ` ` `  `    ``cout << (CountTotal(s)) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count all subStrings ` `// in a String which contains only vowels ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to check if a ` `// character is vowel or not ` `static` `boolean` `isvowel(``char` `ch) ` `{ ` `    ``return` `(ch == ``'a'` `|| ch == ``'e'` `|| ` `            ``ch == ``'i'` `|| ch == ``'o'` `|| ` `            ``ch == ``'u'``); ` `} ` ` `  `// Function to check whether ` `// String contains only vowel ` `static` `boolean` `isvalid(String s) ` `{ ` `    ``int` `n = s.length(); ` ` `  `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{  ` `         `  `       ``// Check if the character is ` `       ``// not vowel then invalid ` `       ``if` `(!isvowel(s.charAt(i))) ` `           ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Function to Count all subStrings ` `// in a String which contains ` `// only vowels ` `static` `int` `CountTotal(String s) ` `{ ` `    ``int` `ans = ``0``; ` `    ``int` `n = s.length(); ` ` `  `    ``// Generate all subString of s ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``String temp = ``""``; ` `       ``for``(``int` `j = i; j < n; j++) ` `       ``{ ` `          ``temp += s.charAt(j); ` `           `  `          ``// If temp contains only vowels ` `          ``if` `(isvalid(temp)) ` `               `  `              ``// Increment the count ` `              ``ans += ``1``; ` `       ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String s = ``"aeoibsddaaeiouudb"``; ` ` `  `    ``System.out.print((CountTotal(s)) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 program to count all substrings ` `# in a string which contains only vowels ` ` `  `# Function to check if a ` `# character is vowel or not ` `def` `isvowel(ch): ` `    ``return` `(ch ``=``=` `'a'` `or` `ch ``=``=` `'e'` `or`  `            ``ch ``=``=` `'i'` `or` `ch ``=``=` `'o'` `or` `            ``ch ``=``=` `'u'``) ` ` `  `# Function to check whether ` `# string contains only vowel ` `def` `isvalid(s): ` `     `  `    ``n ``=` `len``(s) ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# Check if the character is ` `        ``# not vowel then invalid ` `        ``if` `(``not` `isvowel(s[i])): ` `            ``return` `False` `             `  `    ``return` `True` ` `  `# Function to Count all substrings ` `# in a string which contains ` `# only vowels ` `def` `CountTotal(s): ` `     `  `    ``ans ``=` `0` `    ``n ``=` `len``(s) ` ` `  `    ``# Generate all substring of s ` `    ``for` `i ``in` `range``(n): ` `        ``temp ``=` `"" ` `         `  `        ``for` `j ``in` `range``(i, n): ` `            ``temp ``+``=` `s[j] ` ` `  `            ``# If temp contains only vowels ` `            ``if` `(isvalid(temp)): ` `                 `  `                ``# Increment the count ` `                ``ans ``+``=` `1` `                 `  `    ``return` `ans ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``s ``=` `"aeoibsddaaeiouudb"` ` `  `    ``print``(CountTotal(s)) ` ` `  `# This code is contributed by mohit kumar 29  `

## C#

 `// C# program to count all subStrings ` `// in a String which contains only vowels ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to check if a ` `// character is vowel or not ` `static` `Boolean isvowel(``char` `ch) ` `{ ` `    ``return` `(ch == ``'a'` `|| ch == ``'e'` `|| ` `            ``ch == ``'i'` `|| ch == ``'o'` `|| ` `            ``ch == ``'u'``); ` `} ` ` `  `// Function to check whether ` `// String contains only vowel ` `static` `Boolean isvalid(``string` `s) ` `{ ` `    ``int` `n = s.Length; ` ` `  `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{  ` `         `  `        ``// Check if the character is ` `        ``// not vowel then invalid ` `        ``if` `(!isvowel(s[i])) ` `            ``return` `false``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Function to Count all subStrings ` `// in a String which contains ` `// only vowels ` `static` `int` `CountTotal(``string` `s) ` `{ ` `    ``int` `ans = 0; ` `    ``int` `n = s.Length; ` ` `  `    ``// Generate all subString of s ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``string` `temp = ``""``; ` `        ``for``(``int` `j = i; j < n; j++) ` `        ``{ ` `            ``temp += s[j]; ` `             `  `            ``// If temp contains only vowels ` `            ``if` `(isvalid(temp)) ` `                 `  `                ``// Increment the count ` `                ``ans += 1; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `s = ``"aeoibsddaaeiouudb"``; ` ` `  `    ``Console.Write((CountTotal(s)) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```38
```

Time Complexity: O(N3)

Efficient Approach:
To optimize the above approach, the main idea is to count the length of substring which contain only vowels, say x. Then for every x, the number of possible substrings are x * (x + 1) / 2 which contains only vowels. Repeat this process for every such substring and return the final count.

Below is the implementation of the above approach:

## C++

 `// C++ program to Count all substrings ` `// in a string which contains only vowels ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check vowel or not ` `bool` `isvowel(``char` `ch) ` `{ ` `    ``return` `(ch == ``'a'` `or ch == ``'e'` `            ``or ch == ``'i'` `or ch == ``'o'` `            ``or ch == ``'u'``); ` `} ` ` `  `// Function to Count all substrings ` `// in a string which contains ` `// only vowels ` `int` `CountTotal(string& s) ` `{ ` `    ``int` `ans = 0; ` `    ``int` `n = s.length(); ` `    ``int` `cnt = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// Check if current character ` `        ``// is vowel ` `        ``if` `(isvowel(s[i])) ` `            ``// Increment length of substring ` `            ``cnt += 1; ` ` `  `        ``else` `{ ` `            ``// Calculate possible ` `            ``// substrings of calculated ` `            ``// length ` `            ``ans += (cnt * (cnt + 1) / 2); ` ` `  `            ``// Reset the length ` `            ``cnt = 0; ` `        ``} ` `    ``} ` ` `  `    ``// Add remaining possible ` `    ``// substrings consisting ` `    ``// of vowels occupying ` `    ``// last indices of the string ` `    ``if` `(cnt != 0) { ` `        ``ans += (cnt * (cnt + 1) / 2); ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``string s = ``"geeksforgeeks"``; ` ` `  `    ``cout << (CountTotal(s)) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to Count all substrings ` `// in a string which contains only vowels ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Function to check vowel or not ` `    ``static` `boolean` `isvowel(``char` `x) ` `    ``{ ` `        ``return` `(x == ``'a'` `|| x == ``'e'` `                ``|| x == ``'i'` `|| x == ``'o'` `                ``|| x == ``'u'``); ` `    ``} ` ` `  `    ``// Function to find largest ` `    ``// string which satisy condition ` `    ``static` `int` `CountTotal(String str) ` `    ``{ ` `        ``int` `ans = ``0``; ` ` `  `        ``int` `n = str.length(); ` ` `  `        ``char``[] s = str.toCharArray(); ` ` `  `        ``int` `cnt = ``0``; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `            ``// Check if current character ` `            ``// is vowel ` `            ``if` `(isvowel(s[i])) ` `                ``// Increment count ` `                ``cnt += ``1``; ` ` `  `            ``else` `{ ` `                ``// Count all possible ` `                ``// substrings of calculated ` `                ``// length ` `                ``ans += (cnt * (cnt + ``1``) / ``2``); ` ` `  `                ``// Reset the length ` `                ``cnt = ``0``; ` `            ``} ` `        ``} ` ` `  `        ``// Add remaining possible ` `        ``// substrings consisting ` `        ``// of vowels occupying ` `        ``// last indices of the string ` `        ``if` `(cnt != ``0``) ` ` `  `            ``ans += (cnt * (cnt + ``1``) / ``2``); ` ` `  `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver Program ` `    ``static` `public` `void` `main(String[] args) ` `    ``{ ` `        ``String s = ``"geeksforgeeks"``; ` ` `  `        ``System.out.println(CountTotal(s)); ` `    ``} ` `} `

## Python3

 `# Python3 program to Count all substrings ` `# in a string which contains only vowels ` ` `  `# function to check vowel or not ` `def` `isvowel(ch): ` `    ``return``(ch ``in` `"aeiou"``) ` ` `  ` `  `# Function to Count all substrings  ` `# in a string which contains  ` `# only vowels ` `def` `CountTotal(s): ` `     `  `    ``ans ``=` `0` `    ``n ``=` `len``(s) ` `    ``cnt ``=` `0` `     `  `    ``for` `i ``in` `range``(``0``, n): ` `         `  `        ``# if current character is ` `        ``# vowel ` `        ``if``(isvowel(s[i])): ` `            ``# increment ` `            ``cnt ``+``=` `1` `             `  `                 `  `        ``else``: ` `             `  `            ``# Count all possible  ` `            ``# substring of calculated ` `            ``# length ` `            ``ans ``+``=` `(cnt``*``(cnt ``+` `1``) ``/``/` `2``) ` `                 `  `            ``# Reset the length ` `            ``cnt ``=` `0` ` `  `    ``# Add remaining possible ` `    ``# substrings consisting ` `    ``# of vowels occupying ` `    ``# last indices of the string ` `    ``if``(cnt !``=` `0``): ` `        ``ans ``+``=` `(cnt``*``(cnt ``+` `1``) ``/``/` `2``) ` ` `  `    ``return` `ans ` ` `  `# Driver Program  ` `s ``=` `"geeksforgeeks"` ` `  `print``(CountTotal(s)) `

## C#

 `// C# program to count all substrings ` `// in a string which contains only vowels ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to check vowel or not ` `static` `bool` `isvowel(``char` `x) ` `{ ` `    ``return` `(x == ``'a'` `|| x == ``'e'` `|| ` `            ``x == ``'i'` `|| x == ``'o'` `|| ` `            ``x == ``'u'``); ` `} ` ` `  `// Function to find largest ` `// string which satisy condition ` `static` `int` `CountTotal(``string` `str) ` `{ ` `    ``int` `ans = 0; ` `    ``int` `n = str.Length; ` `    ``char``[] s = str.ToCharArray(); ` `    ``int` `cnt = 0; ` ` `  `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `        `  `       ``// Check if current character ` `       ``// is vowel ` `       ``if` `(isvowel(s[i])) ` `        `  `           ``// Increment count ` `           ``cnt += 1; ` `       ``else`  `       ``{ ` `            `  `           ``// Count all possible ` `           ``// substrings of calculated ` `           ``// length ` `           ``ans += (cnt * (cnt + 1) / 2); ` `            `  `           ``// Reset the length ` `           ``cnt = 0; ` `       ``} ` `    ``} ` `     `  `    ``// Add remaining possible ` `    ``// substrings consisting ` `    ``// of vowels occupying ` `    ``// last indices of the string ` `    ``if` `(cnt != 0) ` `        ``ans += (cnt * (cnt + 1) / 2); ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main(``string``[] args) ` `{ ` `    ``string` `s = ``"geeksforgeeks"``; ` ` `  `    ``Console.Write(CountTotal(s)); ` `} ` `} ` ` `  `// This code is contributed by rock_cool `

Output:

```7
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.