Substrings starting with vowel and ending with consonants and vice versa

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count special strings
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



Output :

9


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.



Improved By : Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.