Skip to content
Related Articles
Substrings starting with vowel and ending with consonants and vice versa
• Difficulty Level : Medium
• Last Updated : 09 Jun, 2021

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++

 `// CPP program to count special strings``#include ``using` `namespace` `std;` `// Returns true if ch is vowel``bool` `isVowel(``char` `ch)``{``    ``return` `(ch == ``'a'` `|| ch == ``'e'` `||``            ``ch == ``'i'` `|| ch == ``'o'` `||``            ``ch == ``'u'``);``}` `// function to check consonant``bool` `isCons(``char` `ch)``{``    ``return` `(ch != ``'a'` `&& ch != ``'e'` `&&``            ``ch != ``'i'` `&& ch != ``'o'` `&&``            ``ch != ``'u'``);``}` `int` `countSpecial(string &str)``{``    ``int` `len = str.length();` `    ``// co[i] is going to store counts``    ``// of consonants from str[len-1]``    ``// to str[i].``    ``// vo[i] is going to store counts``    ``// of vowels from str[len-1]``    ``// to str[i].``    ``int` `co[len + 1];``    ``int` `vo[len + 1];``    ``memset``(co, 0, ``sizeof``(co));``    ``memset``(vo, 0, ``sizeof``(vo));` `    ``// Counting consonants and vowels``    ``// from end of string.``    ``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;``        ``}``    ``}` `    ``// Now we traverse string from beginning``    ``long` `long` `ans = 0;``    ``for` `(``int` `i = 0; i < len; i++)``    ``{``        ``// If vowel, then count of substrings``        ``// starting with str[i] is equal to``        ``// count of consonants after it.``        ``if` `(isVowel(str[i]))``           ``ans = ans + co[i + 1];` `        ``// If consonant, then count of``        ``// substrings starting with str[i]``        ``// is equal to count of vowels``        ``// after it.``        ``else``           ``ans = ans + vo[i + 1];``    ``}` `    ``return` `ans;``}` `// driver program``int` `main()``{``    ``string str = ``"adceba"``;``    ``cout << countSpecial(str);``    ``return` `0;``}`

## Java

 `// Java program to count special strings``class` `GfG``{` `// Returns true if ch is vowel``static` `boolean` `isVowel(``char` `ch)``{``    ``return` `(ch == ``'a'` `|| ch == ``'e'` `||``            ``ch == ``'i'` `|| ch == ``'o'` `||``            ``ch == ``'u'``);``}` `// function to check consonant``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;` `    ``// co[i] is going to store counts``    ``// of consonants from str[len-1]``    ``// to str[i].``    ``// vo[i] is going to store counts``    ``// of vowels from str[len-1]``    ``// to str[i].``    ``int` `co[] = ``new` `int``[len + ``1``];``    ``int` `vo[] = ``new` `int``[len + ``1``];`  `    ``// Counting consonants and vowels``    ``// from end of string.``    ``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``;``        ``}``    ``}` `    ``// Now we traverse string from beginning``    ``long` `ans = ``0``;``    ``for` `(``int` `i = ``0``; i < len; i++)``    ``{``        ``// If vowel, then count of substrings``        ``// starting with str[i] is equal to``        ``// count of consonants after it.``        ``if` `(isVowel(str[i]))``        ``ans = ans + co[i + ``1``];` `        ``// If consonant, then count of``        ``// substrings starting with str[i]``        ``// is equal to count of vowels``        ``// after it.``        ``else``        ``ans = ans + vo[i + ``1``];``    ``}` `    ``return` `(``int``) ans;``}` `// Driver program``public` `static` `void` `main(String[] args)``{``    ``String str = ``"adceba"``;``    ``System.out.println(countSpecial(str.toCharArray()));``}``}` `// This code contributed by Rajput-Ji`

## Python3

 `# Python3 program to count special strings` `# Returns true if ch is vowel``def` `isVowel(ch):``    ``return` `(ch ``=``=` `'a'` `or` `ch ``=``=` `'e'` `or``            ``ch ``=``=` `'i'` `or` `ch ``=``=` `'o'` `or``            ``ch ``=``=` `'u'``)` `# Function to check consonant``def` `isCons( ch):``    ``return` `(ch !``=` `'a'` `and` `ch !``=` `'e'` `and``            ``ch !``=` `'i'` `and` `ch !``=` `'o'` `and``            ``ch !``=` `'u'``)` `def` `countSpecial(``str``):``    ``lent ``=` `len``(``str``)``    ` `    ``# co[i] is going to store counts``    ``# of consonants from str[len-1]``    ``# to str[i].``    ``# vo[i] is going to store counts``    ``# of vowels from str[len-1]``    ``# to str[i].``    ``co ``=` `[]``    ``vo ``=` `[]``    ` `    ``for` `i ``in` `range``(``0``, lent ``+` `1``):``        ``co.append(``0``)``        ` `    ``for` `i ``in` `range``(``0``, lent ``+` `1``):``        ``vo.append(``0``)``    ` `    ``# Counting consonants and vowels``    ``# from end of string.``    ``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` `    ``# Now we traverse string from beginning``    ``ans ``=` `0``    ` `    ``for` `i ``in` `range``(lent):``        ` `        ``#If vowel, then count of substrings``        ``# starting with str[i] is equal to``        ``# count of consonants after it.``        ``if` `isVowel(``str``[i]):``            ``ans ``=` `ans ``+` `co[i ``+` `1``]` `        ``#If consonant, then count of``        ``# substrings starting with str[i]``        ``# is equal to count of vowels``        ``# after it.``        ``else``:``            ``ans ``=` `ans ``+` `vo[i ``+` `1``]` `    ``return` `ans` `# Driver Code``str` `=` `"adceba"``print``(countSpecial(``str``))` `# This code is contributed by Upendra singh bartwal`

## C#

 `// C# program to count special strings``using` `System;` `class` `GFG``{` `// Returns true if ch is vowel``static` `Boolean isVowel(``char` `ch)``{``    ``return` `(ch == ``'a'` `|| ch == ``'e'` `||``            ``ch == ``'i'` `|| ch == ``'o'` `||``            ``ch == ``'u'``);``}` `// function to check consonant``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;` `    ``// co[i] is going to store counts``    ``// of consonants from str[len-1]``    ``// to str[i].``    ``// vo[i] is going to store counts``    ``// of vowels from str[len-1]``    ``// to str[i].``    ``int` `[]co = ``new` `int``[len + 1];``    ``int` `[]vo = ``new` `int``[len + 1];` `    ``// Counting consonants and vowels``    ``// from end of string.``    ``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;``        ``}``    ``}` `    ``// Now we traverse string from beginning``    ``long` `ans = 0;``    ``for` `(``int` `i = 0; i < len; i++)``    ``{``        ``// If vowel, then count of substrings``        ``// starting with str[i] is equal to``        ``// count of consonants after it.``        ``if` `(isVowel(str[i]))``        ``ans = ans + co[i + 1];` `        ``// If consonant, then count of``        ``// substrings starting with str[i]``        ``// is equal to count of vowels``        ``// after it.``        ``else``        ``ans = ans + vo[i + 1];``    ``}``    ``return` `(``int``) ans;``}` `// Driver program``public` `static` `void` `Main(String[] args)``{``    ``String str = ``"adceba"``;``    ``Console.WriteLine(countSpecial(str.ToCharArray()));``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output :

`9`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up