Substrings starting with vowel and ending with consonants and vice versa
Last Updated :
22 May, 2022
Given a string s, count special substrings in it. A Substring of S is said to be special if either of the following properties is satisfied.
- It starts with a vowel and ends with a consonant.
- It starts with a consonant and ends with a vowel.
Examples:
Input : S = "aba"
Output : 2
Substrings of S are : a, ab, aba, b, ba, a
Out of these only 'ab' and 'ba' satisfy the
condition for special Substring. So the
answer is 2.
Input : S = "adceba"
Output : 9
A simple solution is to generate all substrings. For every substring check the condition of special string. If yes increment count.
An efficient solution is to count vowels and consonants in every suffix of string. After counting these, we traverse string from beginning. For every consonant, we add number of vowels after it to result. Similarly, for every vowel, we add number of consonants after it.
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
bool isCons( char ch)
{
return (ch != 'a' && ch != 'e' &&
ch != 'i' && ch != 'o' &&
ch != 'u' );
}
int countSpecial(string &str)
{
int len = str.length();
if (len == 0)
return 0;
int co[len + 1];
int vo[len + 1];
memset (co, 0, sizeof (co));
memset (vo, 0, sizeof (vo));
if (isCons(str[len - 1]) == 1)
co[len-1] = 1;
else
vo[len-1] = 1;
for ( int i = len-2; i >= 0; i--)
{
if (isCons(str[i]) == 1)
{
co[i] = co[i + 1] + 1;
vo[i] = vo[i + 1];
}
else
{
co[i] = co[i + 1];
vo[i] = vo[i + 1] + 1;
}
}
long long ans = 0;
for ( int i = 0; i < len; i++)
{
if (isVowel(str[i]))
ans = ans + co[i + 1];
else
ans = ans + vo[i + 1];
}
return ans;
}
int main()
{
string str = "adceba" ;
cout << countSpecial(str);
return 0;
}
|
Java
class GfG
{
static boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static boolean isCons( char ch)
{
return (ch != 'a' && ch != 'e' &&
ch != 'i' && ch != 'o' &&
ch != 'u' );
}
static int countSpecial( char []str)
{
int len = str.length;
if (len == 0 )
return 0 ;
int co[] = new int [len + 1 ];
int vo[] = new int [len + 1 ];
if (isCons(str[len - 1 ]) == true )
co[len- 1 ] = 1 ;
else
vo[len- 1 ] = 1 ;
for ( int i = len- 2 ; i >= 0 ; i--)
{
if (isCons(str[i]) == true )
{
co[i] = co[i + 1 ] + 1 ;
vo[i] = vo[i + 1 ];
}
else
{
co[i] = co[i + 1 ];
vo[i] = vo[i + 1 ] + 1 ;
}
}
long ans = 0 ;
for ( int i = 0 ; i < len; i++)
{
if (isVowel(str[i]))
ans = ans + co[i + 1 ];
else
ans = ans + vo[i + 1 ];
}
return ( int ) ans;
}
public static void main(String[] args)
{
String str = "adceba" ;
System.out.println(countSpecial(str.toCharArray()));
}
}
|
Python3
def isVowel(ch):
return (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' )
def isCons( ch):
return (ch ! = 'a' and ch ! = 'e' and
ch ! = 'i' and ch ! = 'o' and
ch ! = 'u' )
def countSpecial( str ):
lent = len ( str )
if lent = = 0 :
return 0 ;
co = []
vo = []
for i in range ( 0 , lent + 1 ):
co.append( 0 )
for i in range ( 0 , lent + 1 ):
vo.append( 0 )
if isCons( str [lent - 1 ]) = = 1 :
co[lent - 1 ] = 1
else :
vo[lent - 1 ] = 1
for i in range (lent - 2 , - 1 , - 1 ):
if isCons( str [i]) = = 1 :
co[i] = co[i + 1 ] + 1
vo[i] = vo[i + 1 ]
else :
co[i] = co[i + 1 ]
vo[i] = vo[i + 1 ] + 1
ans = 0
for i in range (lent):
if isVowel( str [i]):
ans = ans + co[i + 1 ]
else :
ans = ans + vo[i + 1 ]
return ans
str = "adceba"
print (countSpecial( str ))
|
C#
using System;
class GFG
{
static Boolean isVowel( char ch)
{
return (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' );
}
static Boolean isCons( char ch)
{
return (ch != 'a' && ch != 'e' &&
ch != 'i' && ch != 'o' &&
ch != 'u' );
}
static int countSpecial( char []str)
{
int len = str.Length;
if (len == 0)
return 0;
int []co = new int [len + 1];
int []vo = new int [len + 1];
if (isCons(str[len - 1]) == true )
co[len - 1] = 1;
else
vo[len - 1] = 1;
for ( int i = len - 2; i >= 0; i--)
{
if (isCons(str[i]) == true )
{
co[i] = co[i + 1] + 1;
vo[i] = vo[i + 1];
}
else
{
co[i] = co[i + 1];
vo[i] = vo[i + 1] + 1;
}
}
long ans = 0;
for ( int i = 0; i < len; i++)
{
if (isVowel(str[i]))
ans = ans + co[i + 1];
else
ans = ans + vo[i + 1];
}
return ( int ) ans;
}
public static void Main(String[] args)
{
String str = "adceba" ;
Console.WriteLine(countSpecial(str.ToCharArray()));
}
}
|
Javascript
<script>
function isVowel(ch)
{
return (ch === "a" || ch === "e" ||
ch === "i" || ch === "o" ||
ch === "u" );
}
function isCons(ch)
{
return (ch !== "a" && ch !== "e" &&
ch !== "i" && ch !== "o" &&
ch !== "u" );
}
function countSpecial(str)
{
var len = str.length;
if (len == 0)
return 0;
var co = new Array(len + 1).fill(0);
var vo = new Array(len + 1).fill(0);
if (isCons(str[len - 1]) === true ) co[len - 1] = 1;
else vo[len - 1] = 1;
for ( var i = len - 2; i >= 0; i--)
{
if (isCons(str[i]) === true )
{
co[i] = co[i + 1] + 1;
vo[i] = vo[i + 1];
}
else
{
co[i] = co[i + 1];
vo[i] = vo[i + 1] + 1;
}
}
var ans = 0;
for ( var i = 0; i < len; i++)
{
if (isVowel(str[i])) ans = ans + co[i + 1];
else ans = ans + vo[i + 1];
}
return parseInt(ans);
}
var str = "adceba" ;
document.write(countSpecial(str.split( "" )));
</script>
|
Time Complexity : O( |str| ) , as we make a linear traversal to count the number of vowels and consonants.
Space Complexity for above solution : O( |str| ), as we make two arrays vo[] and co[] to store the number of vowels at any index i.
The above solution can be optimized to be done in O(1) space complexity, by using only two variables instead of making two arrays to store vowels and consonants at any index i. Following code depicts the same :
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' )
return true ;
return false ;
}
long long countSpecial(string str)
{
long long cnt = 0;
int n = str.size();
if (n == 1 || n == 0)
return 0;
long long vow = 0, cons = 0;
for ( int i = 0; i < n; i++)
vow += isVowel(str[i]);
cons = n - vow;
for ( int i = 0; i < n; i++) {
if (isVowel(str[i])) {
vow--;
cnt += cons;
}
else {
cons--;
cnt += vow;
}
}
return cnt;
}
int main()
{
string str = "adceba" ;
cout << countSpecial(str);
return 0;
}
|
C
#include <stdio.h>
int isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' )
return 1;
return 0;
}
long long countSpecial( char str[], int n)
{
long long cnt = 0;
if (n == 1 || n == 0)
return 0;
long long vow = 0, cons = 0;
for ( int i = 0; i < n; i++)
vow += isVowel(str[i]);
cons = n - vow;
for ( int i = 0; i < n; i++) {
if (isVowel(str[i])) {
vow--;
cnt += cons;
}
else {
cons--;
cnt += vow;
}
}
return cnt;
}
int main()
{
char str[] = { 'a' , 'd' , 'c' , 'e' , 'b' , 'a' };
int n = sizeof (str) / sizeof ( char );
if (n == 0) {
printf ( "0" );
}
else {
long long count = countSpecial(str, n);
printf ( "%lld" , count);
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' )
return 1 ;
return 0 ;
}
public static long countSpecial(String str)
{
long cnt = 0 ;
int n = str.length();
if (n == 1 || n == 0 )
return 0 ;
long vow = 0 , cons = 0 ;
for ( int i = 0 ; i < n; i++) {
char ch = str.charAt(i);
vow += isVowel(ch);
}
cons = n - vow;
for ( int i = 0 ; i < n; i++) {
char ch = str.charAt(i);
if (isVowel(ch) == 1 ) {
vow--;
cnt += cons;
}
else {
cons--;
cnt += vow;
}
}
return cnt;
}
public static void main(String[] args)
{
String str = "adceba" ;
long count = countSpecial(str);
System.out.println(count);
}
}
|
Python3
def isVowel(ch):
if (ch = = 'a' or ch = = 'e' or ch = = 'i' or ch = = 'o' or ch = = 'u' ):
return 1 ;
return 0 ;
def countSpecial( str ):
cnt = 0 ;
n = len ( str );
if (n = = 1 or n = = 0 ):
return 0 ;
vow = 0
cons = 0 ;
for i in range (n):
ch = str [i];
vow + = isVowel(ch);
cons = n - vow;
for i in range (n):
ch = str [i];
if (isVowel(ch) = = 1 ):
vow - = 1 ;
cnt + = cons;
else :
cons - = 1 ;
cnt + = vow;
return cnt;
if __name__ = = '__main__' :
str = "adceba" ;
count = countSpecial( str );
print (count);
|
C#
using System;
class GFG
{
public static int isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' )
return 1;
return 0;
}
public static long countSpecial(String str)
{
long cnt = 0;
int n = str.Length;
if (n == 1 || n == 0)
return 0;
long vow = 0, cons = 0;
for ( int i = 0; i < n; i++) {
char ch = str[i];
vow += isVowel(ch);
}
cons = n - vow;
for ( int i = 0; i < n; i++) {
char ch = str[i];
if (isVowel(ch) == 1) {
vow--;
cnt += cons;
}
else {
cons--;
cnt += vow;
}
}
return cnt;
}
public static void Main(String[] args)
{
string str = "adceba" ;
long count = countSpecial(str);
Console.Write(count);
}
}
|
Javascript
<script>
function isVowel(ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' )
return 1;
return 0;
}
function countSpecial(str)
{
var cnt = 0;
var n = str.length;
if (n == 1 || n == 0)
return 0;
var vow = 0, cons = 0;
for ( var i = 0; i < n; i++) {
var ch = str.charAt(i);
vow += isVowel(ch);
}
cons = n - vow;
for ( var i = 0; i < n; i++) {
var ch = str.charAt(i);
if (isVowel(ch) == 1) {
vow--;
cnt += cons;
}
else {
cons--;
cnt += vow;
}
}
return cnt;
}
var str = "adceba" ;
var count = countSpecial(str);
document.write(count);
</script>
|
Time Complexity : O( |str| )
Space Complexity : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...