Count of substrings consisting only of vowels
Given a string S, the task is to count all substrings which contain 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.
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++
#include <iostream>
using namespace std;
bool isvowel( char ch)
{
return (ch == 'a' or ch == 'e'
or ch == 'i' or ch == 'o'
or ch == 'u' );
}
bool isvalid(string& s)
{
int n = s.length();
for ( int i = 0; i < n; i++) {
if (!isvowel(s[i]))
return false ;
}
return true ;
}
int CountTotal(string& s)
{
int ans = 0;
int n = s.length();
for ( int i = 0; i < n; i++) {
string temp = "" ;
for ( int j = i; j < n; j++) {
temp += s[j];
if (isvalid(temp))
ans += 1;
}
}
return ans;
}
int main()
{
string s = "aeoibsddaaeiouudb" ;
cout << (CountTotal(s)) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isvowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static boolean isvalid(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
if (!isvowel(s.charAt(i)))
return false ;
}
return true ;
}
static int CountTotal(String s)
{
int ans = 0 ;
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
String temp = "" ;
for ( int j = i; j < n; j++)
{
temp += s.charAt(j);
if (isvalid(temp))
ans += 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
String s = "aeoibsddaaeiouudb" ;
System.out.print((CountTotal(s)) + "\n" );
}
}
|
Python3
def isvowel(ch):
return (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' )
def isvalid(s):
n = len (s)
for i in range (n):
if ( not isvowel(s[i])):
return False
return True
def CountTotal(s):
ans = 0
n = len (s)
for i in range (n):
temp = ""
for j in range (i, n):
temp + = s[j]
if (isvalid(temp)):
ans + = 1
return ans
if __name__ = = '__main__' :
s = "aeoibsddaaeiouudb"
print (CountTotal(s))
|
C#
using System;
class GFG{
static Boolean isvowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static Boolean isvalid( string s)
{
int n = s.Length;
for ( int i = 0; i < n; i++)
{
if (!isvowel(s[i]))
return false ;
}
return true ;
}
static int CountTotal( string s)
{
int ans = 0;
int n = s.Length;
for ( int i = 0; i < n; i++)
{
string temp = "" ;
for ( int j = i; j < n; j++)
{
temp += s[j];
if (isvalid(temp))
ans += 1;
}
}
return ans;
}
public static void Main()
{
string s = "aeoibsddaaeiouudb" ;
Console.Write((CountTotal(s)) + "\n" );
}
}
|
Javascript
<script>
function isvowel(ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
function isvalid(s)
{
let n = s.length;
for (let i = 0; i < n; i++)
{
if (!isvowel(s[i]))
return false ;
}
return true ;
}
function CountTotal(s)
{
let ans = 0;
let n = s.length;
for (let i = 0; i < n; i++)
{
let temp = "" ;
for (let j = i; j < n; j++)
{
temp += s[j];
if (isvalid(temp))
ans += 1;
}
}
return ans;
}
let s = "aeoibsddaaeiouudb" ;
document.write((CountTotal(s)));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(N), where N is the length of the given string.
Efficient Approach:
To optimize the above approach, the main idea is to count the length of the 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++
#include <iostream>
using namespace std;
bool isvowel( char ch)
{
return (ch == 'a' or ch == 'e'
or ch == 'i' or ch == 'o'
or ch == 'u' );
}
int CountTotal(string& s)
{
int ans = 0;
int n = s.length();
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (isvowel(s[i]))
cnt += 1;
else {
ans += (cnt * (cnt + 1) / 2);
cnt = 0;
}
}
if (cnt != 0) {
ans += (cnt * (cnt + 1) / 2);
}
return ans;
}
int main()
{
string s = "geeksforgeeks" ;
cout << (CountTotal(s)) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
static boolean isvowel( char x)
{
return (x == 'a' || x == 'e'
|| x == 'i' || x == 'o'
|| x == 'u' );
}
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++) {
if (isvowel(s[i]))
cnt += 1 ;
else {
ans += (cnt * (cnt + 1 ) / 2 );
cnt = 0 ;
}
}
if (cnt != 0 )
ans += (cnt * (cnt + 1 ) / 2 );
return ans;
}
static public void main(String[] args)
{
String s = "geeksforgeeks" ;
System.out.println(CountTotal(s));
}
}
|
Python3
def isvowel(ch):
return (ch in "aeiou" )
def CountTotal(s):
ans = 0
n = len (s)
cnt = 0
for i in range ( 0 , n):
if (isvowel(s[i])):
cnt + = 1
else :
ans + = (cnt * (cnt + 1 ) / / 2 )
cnt = 0
if (cnt ! = 0 ):
ans + = (cnt * (cnt + 1 ) / / 2 )
return ans
s = "geeksforgeeks"
print (CountTotal(s))
|
C#
using System;
class GFG{
static bool isvowel( char x)
{
return (x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' );
}
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++)
{
if (isvowel(s[i]))
cnt += 1;
else
{
ans += (cnt * (cnt + 1) / 2);
cnt = 0;
}
}
if (cnt != 0)
ans += (cnt * (cnt + 1) / 2);
return ans;
}
static public void Main( string [] args)
{
string s = "geeksforgeeks" ;
Console.Write(CountTotal(s));
}
}
|
Javascript
<script>
function isvowel(x)
{
return (x == 'a' || x == 'e' ||
x == 'i' || x == 'o' ||
x == 'u' );
}
function CountTotal(str)
{
let ans = 0;
let n = str.length;
let s = str.split( '' );
let cnt = 0;
for (let i = 0; i < n; i++)
{
if (isvowel(s[i]))
cnt += 1;
else
{
ans += (cnt * (cnt + 1) / 2);
cnt = 0;
}
}
if (cnt != 0)
ans += (cnt * (cnt + 1) / 2);
return ans;
}
let s = "geeksforgeeks" ;
document.write(CountTotal(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
02 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...