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++
// 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; } |
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 |
Output :
9
Recommended Posts:
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Arrangement of words without changing the relative position of vowel and consonants
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Replace consonants with next immediate consonants alphabetically in a String
- Count of words ending at the given suffix in Java
- Alternate vowel and consonant string
- Check if the given string is vowel prime
- Lexicographically first alternate vowel and consonant string
- Count of adjacent Vowel Consonant Pairs
- Python program to check if given string is vowel Palindrome
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Modify string by removing vowels in between two consonants
- Replace all consonants with nearest vowels in a string
- Count strings with consonants and vowels at alternate position
- Count consonants in a string (Iterative and recursive methods)
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



