Check whether all the substrings have number of vowels atleast as that of consonants
Given a string str, the task is to check whether all the substrings of length ≥ 2 have the number of vowels at least as that of the number of consonants.
Examples:
Input: str = “acaba”
Output: No
The substring “cab” has 2 consonants and a single vowel.
Input: str = “aabaa”
Output: Yes
Approach: There are only two cases where the given condition is not satisfied:
- When there are two consecutive consonants as in this case a substring of size 2 can have 2 consonants and no vowels.
- When there is a vowel surrounded by two consonants, in this case a substring of length 3 is possible with 2 consonants and 1 vowels.
All the other cases will always satisfy the given conditions.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true // if character ch is a vowel bool isVowel(char ch) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; } return false; } // Compares two integers according // to their digit sum bool isSatisfied(string str, int n) { // Check if there are two // consecutive consonants for (int i = 1; i < n; i++) { if (!isVowel(str[i]) && !isVowel(str[i - 1])) { return false; } } // Check if there is any vowel // surrounded by two consonants for (int i = 1; i < n - 1; i++) { if (isVowel(str[i]) && !isVowel(str[i - 1]) && !isVowel(str[i + 1])) { return false; } } return true; } // Driver code int main() { string str = "acaba"; int n = str.length(); if (isSatisfied(str, n)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class GFG { // Function that returns true // if character ch is a vowel static boolean isVowel(char ch) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; } return false; } // Compares two integers according // to their digit sum static boolean isSatisfied(char[] str, int n) { // Check if there are two // consecutive consonants for (int i = 1; i < n; i++) { if (!isVowel(str[i]) && !isVowel(str[i - 1])) { return false; } } // Check if there is any vowel // surrounded by two consonants for (int i = 1; i < n - 1; i++) { if (isVowel(str[i]) && !isVowel(str[i - 1]) && !isVowel(str[i + 1])) { return false; } } return true; } // Driver code public static void main(String []args) { String str = "acaba"; int n = str.length(); if (isSatisfied(str.toCharArray(), n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function that returns true # if acter ch is a vowel def isVowel(ch): if ch in ['i', 'a', 'e', 'o', 'u']: return True else: return False # Compares two integers according # to their digit sum def isSatisfied(st, n): # Check if there are two # consecutive consonants for i in range(1, n): if (isVowel(st[i]) == False and isVowel(st[i - 1]) == False): return False # Check if there is any vowel # surrounded by two consonants for i in range(1, n - 1): if (isVowel(st[i]) and isVowel(st[i - 1]) == False and isVowel(st[i + 1]) == False ): return False return True # Driver code st = "acaba"n = len(st) if (isSatisfied(st, n)): print("Yes") else: print("No") # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true // if character ch is a vowel static bool isVowel(char ch) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; } return false; } // Compares two integers according // to their digit sum static bool isSatisfied(char[] str, int n) { // Check if there are two // consecutive consonants for (int i = 1; i < n; i++) { if (!isVowel(str[i]) && !isVowel(str[i - 1])) { return false; } } // Check if there is any vowel // surrounded by two consonants for (int i = 1; i < n - 1; i++) { if (isVowel(str[i]) && !isVowel(str[i - 1]) && !isVowel(str[i + 1])) { return false; } } return true; } // Driver code public static void Main(String []args) { String str = "acaba"; int n = str.Length; if (isSatisfied(str.ToCharArray(), n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Rajput-Ji |
No
Recommended Posts:
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Encrypt string with product of number of vowels and consonants in substring of size k
- Check if a string can be converted to another string by replacing vowels and consonants
- Count the number of vowels occurring in all the substrings of given string
- Replace all consonants with nearest vowels in a string
- Modify string by removing vowels in between two consonants
- Modify the string by swapping continuous vowels or consonants
- Count strings with consonants and vowels at alternate position
- Arrange consonants and vowels nodes in a linked list
- Substrings starting with vowel and ending with consonants and vice versa
- Count substrings that contain all vowels | SET 2
- Find substrings that contain all vowels
- Queries to find the count of vowels in the substrings of the given string
- Replace consonants with next immediate consonants alphabetically in a String
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.


