Given a string S of length N, the task is to find the number of non-empty substrings having even number of vowels.
Examples:
Input: N = 5, S = “abcde”
Output: 7
Explanation:
All possible substrings with even number of vowels are:
Substring Vowels
{abcde} 2
{b} 0
{bc} 0
{bcd} 0
{c} 0
{cd} 0
{d} 0
Input: N=4, S=”geeks”
Output: 6
Naive Approach:
The simplest approach to solve the problem is to generate all possible substrings of the given string and for each substring, count the number of vowels and check if it is even or not. If found to be even, increase count. Finally, after checking for all substrings, print the value of count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
return true ;
return false ;
}
void countSubstrings(string s, int n)
{
int result = 0;
for ( int i = 0; i < n; i++)
{
int count = 0;
for ( int j = i; j < n; j++)
{
if (isVowel(s[j]))
{
count++;
}
if (count % 2 == 0)
result++;
}
}
cout << result;
}
int main()
{
int n = 5;
string s = "abcde" ;
countSubstrings(s, n);
return 0;
}
|
Java
class GFG {
static boolean isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
static void countSubstrings(String s, int n)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
int count = 0 ;
for ( int j = i; j < n; j++) {
if (isVowel(s.charAt(j))) {
count++;
}
if (count % 2 == 0 )
result++;
}
}
System.out.println(result);
}
public static void main(String[] args)
{
int n = 5 ;
String s = "abcde" ;
countSubstrings(s, n);
}
}
|
Python3
def isVowel(c):
if (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' ):
return True
return False
def countSubstrings(s, n):
result = 0
for i in range (n):
count = 0
for j in range (i, n):
if (isVowel(s[j])):
count + = 1
if (count % 2 = = 0 ):
result + = 1
print (result)
if __name__ = = '__main__' :
n = 5
s = "abcde"
countSubstrings(s, n)
|
C#
using System;
class GFG{
static bool isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' )
return true ;
return false ;
}
static void countSubstrings(String s, int n)
{
int result = 0;
for ( int i = 0; i < n; i++)
{
int count = 0;
for ( int j = i; j < n; j++)
{
if (isVowel(s[j]))
{
count++;
}
if (count % 2 == 0)
result++;
}
}
Console.WriteLine(result);
}
public static void Main(String[] args)
{
int n = 5;
String s = "abcde" ;
countSubstrings(s, n);
}
}
|
Javascript
<script>
function isVowel(c)
{
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
return true ;
return false ;
}
function countSubstrings(s, n)
{
let result = 0;
for (let i = 0; i < n; i++)
{
let count = 0;
for (let j = i; j < n; j++)
{
if (isVowel(s[j]))
{
count++;
}
if (count % 2 == 0)
result++;
}
}
document.write(result);
}
let n = 5;
let s = "abcde" ;
countSubstrings(s, n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
Follow the steps below to optimize the above approach:
- Initialize a cumulative count modulo array temp[], such that:
temp[0] : Stores count of substrings having even number of vowels.
temp[1] : Stores count of substrings having odd number of vowels.
- Any substring [Si, Sj] will contain even number of vowels if the number of vowels in [S0, Si-1], and the number of vowels in [S0, Sj] have the same parity, i.e. either they are both even or both odd.
- Since the count of substrings with even number of vowels and odd number of vowels are stored in temp[], then, by using handshaking lemma:
Total count of substrings = (temp[0] * (temp[0] – 1))/2 + (temp[1] * (temp[1] – 1))/2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
return true ;
return false ;
}
void countSubstrings(string s, int n)
{
int temp[] = { 1, 0 };
int result = 0, sum = 0;
for ( int i = 0; i <= n - 1; i++)
{
sum += (isVowel(s[i]) ? 1 : 0);
sum %= 2;
temp[sum]++;
}
result += ((temp[0] * (temp[0] - 1)) / 2);
result += ((temp[1] * (temp[1] - 1)) / 2);
cout << result;
}
int main()
{
int n = 5;
string s = "abcde" ;
countSubstrings(s, n);
}
|
Java
class GFG {
static boolean isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
static void countSubstrings(String s, int n)
{
int temp[] = { 1 , 0 };
int result = 0 , sum = 0 ;
for ( int i = 0 ; i <= n - 1 ; i++) {
sum += (isVowel(s.charAt(i)) ? 1 : 0 );
sum %= 2 ;
temp[sum]++;
}
result += ((temp[ 0 ] * (temp[ 0 ] - 1 )) / 2 );
result += ((temp[ 1 ] * (temp[ 1 ] - 1 )) / 2 );
System.out.println(result);
}
public static void main(String[] args)
{
int n = 5 ;
String s = "abcde" ;
countSubstrings(s, n);
}
}
|
Python3
def isVowel(c):
if (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' ):
return True ;
return False ;
def countSubstrings(s, n):
temp = [ 1 , 0 ];
result = 0 ;
sum = 0 ;
for i in range ( 0 , n):
sum + = ( 1 if isVowel(s[i]) else 0 );
sum % = 2 ;
temp[ sum ] + = 1 ;
result + = ((temp[ 0 ] * (temp[ 0 ] - 1 )) / / 2 );
result + = ((temp[ 1 ] * (temp[ 1 ] - 1 )) / / 2 );
print (result);
if __name__ = = '__main__' :
n = 5 ;
s = "abcde" ;
countSubstrings(s, n);
|
C#
using System;
class GFG {
static bool isVowel( char c)
{
if (c == 'a' || c == 'e' || c == 'i' ||
c == 'o' || c == 'u' )
return true ;
return false ;
}
static void countSubstrings(String s, int n)
{
int []temp = { 1, 0 };
int result = 0, sum = 0;
for ( int i = 0; i <= n - 1; i++)
{
sum += (isVowel(s[i]) ? 1 : 0);
sum %= 2;
temp[sum]++;
}
result += ((temp[0] * (temp[0] - 1)) / 2);
result += ((temp[1] * (temp[1] - 1)) / 2);
Console.Write(result);
}
public static void Main( string [] args)
{
int n = 5;
String s = "abcde" ;
countSubstrings(s, n);
}
}
|
Javascript
<script>
function isVowel(c)
{
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
return true ;
return false ;
}
function countSubstrings(s, n)
{
let temp = [ 1, 0 ];
let result = 0, sum = 0;
for (let i = 0; i <= n - 1; i++)
{
sum += (isVowel(s[i]) ? 1 : 0);
sum %= 2;
temp[sum]++;
}
result += ((temp[0] * (temp[0] - 1)) / 2);
result += ((temp[1] * (temp[1] - 1)) / 2);
document.write(result);
}
let n = 5;
let s = "abcde" ;
countSubstrings(s, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)