Consonant and Vowel group check in String with Wildcard characters Last Updated : 26 Oct, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string S which is composed of lowercase alphabets and wildcard characters i.e. '?'. Here, '?' can be replaced by any of the lowercase alphabets, the task is to return 'True' if there are more than 3 consonants together or more than 5 vowels together else 'False'. Examples: Input: S = aeioup??Output: 0 Input: S = bcdaeiou??Output: 1 Approach: Follow the steps to solve the problem: Create a boolean function that will check whether a character is a vowel or not.Initialize the count of vowels & consonants as 0.Traverse through the string and do the following:If a character is a vowel then increase v, if it is a '?' then increase both v and c else increase only c.If the count of v exceeds 5 or the count of c exceeds 3 then print '0' else print '1'.Below is the implementation for the above approach: C++ // C++ code for the above approach: #include <iostream> #include <string> using namespace std; bool vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { return true; } return false; } bool isValid(string S) { int v = 0, c = 0; for (int i = 0; i < S.length(); i++) { char ch = S[i]; if (vowel(ch)) { c = 0; v++; } else if (ch == '?') { v++; c++; } else { v = 0; c++; } if (v > 5 || c > 3) { return true; } } return false; } // Drivers code int main() { string s = "bcdaeiou??"; // Function Call cout << isValid(s) << endl; return 0; } C #include <stdio.h> #include <stdbool.h> bool vowel(char c) { if(c=='a' || c=='e' || c=='i' || c=='o'|| c=='u') { return true; } return false; } char* isGoodorBad(char* S) { int v=0,c=0; for(int i=0;S[i]!='\0';i++) { char ch=S[i]; if(vowel(ch)) { c=0; v++; } else if(ch=='?') { v++; c++; } else { v=0; c++; } if(v>5 || c>3) { return "String is Bad"; } } return "String is Good"; } int main() { char s[] = "bcdaeiou??"; printf("%s\n", isGoodorBad(s)); return 0; } Java import java.io.*; class GFG { public static void main(String[] args) { String s = "bcdaeiou??"; System.out.println(isGoodorBad(s)); } static boolean vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { return true; } return false; } static String isGoodorBad(String S) { int v = 0, c = 0; for (int i = 0; i < S.length(); i++) { char ch = S.charAt(i); if (vowel(ch)) { c = 0; v++; } else if (ch == '?') { v++; c++; } else { v = 0; c++; } if (v > 5 || c > 3) { return "String is Bad"; } } return "String is Good"; } } Python3 def main(): s = "bcdaeiou??" print(is_good_or_bad(s)) def vowel(c): if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u': return True return False def is_good_or_bad(S): v = 0 c = 0 for i in range(len(S)): ch = S[i] if vowel(ch): c = 0 v += 1 elif ch == '?': v += 1 c += 1 else: v = 0 c += 1 if v > 5 or c > 3: return "String is Bad" return "String is Good" if __name__ == "__main__": main() C# using System; class Program { static void Main() { string s = "bcdaeiou??"; Console.WriteLine(IsGoodOrBad(s)); } // Boolean function that check character is vowel or not static bool Vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { return true; } return false; } static string IsGoodOrBad(string S) { // initial variable to store vowel and // consonant number int v = 0; int c = 0; // iterate over string for (int i = 0; i < S.Length; i++) { // Check character is vowel or not char ch = S[i]; if (Vowel(ch)) { // If vowel increse v // and assign cosonant to 0 c = 0; v += 1; } else if (ch == '?') { // if character is '?' then increase // both vowel and consonant count by 1 v += 1; c += 1; } else { // Else increase consonant by 1 and // make vowel 0 v = 0; c += 1; } // Check condition for bad string if (v > 5 || c > 3) { return "String is Bad"; } } return "String is Good"; } } JavaScript // function to check whether character is vowel or not function vowel(c) { if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u') { return true; } return false; } function isValid(S) { // initalize vowel and consonants as 0 let v = 0, c = 0; // Traverse through the string for (let i = 0; i < S.length; i++) { let ch = S[i]; if (vowel(ch)) { c = 0; v++; } else if (ch === '?') { v++; c++; } else { v = 0; c++; } // If count of vowel exceeds 5 or count of consonants exceeds 3 then // Return Bad string else return Good string if (v > 5 || c > 3) { return "String is Bad"; } } return "String is Good"; } let s = "bcdaeiou??"; console.log(isValid(s)); Output0Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count strings with consonants and vowels at alternate position G geek_suryansh Follow Improve Article Tags : Strings DSA Data Structures strings Practice Tags : Data StructuresStringsStrings Similar Reads String matching where one string contains wildcard characters Given two strings where first string may contain wild card characters and second string is a normal string. Write a function that returns true if the two strings match. The following are allowed wild card characters in first string. * --> Matches with 0 or more instances of any character or set o 9 min read Program to count vowels, consonant, digits and special characters in string. Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels: 6 min read Count strings with consonants and vowels at alternate position Given a string str. The task is to find all possible number of strings that can be obtained by replacing the "$" with alphabets in the given string. Note: Alphabets should be placed in such a way that the string is always alternating in vowels and consonants, and the string must always start with a 6 min read 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 : 15+ min read Check if a string consists only of special characters Given string str of length N, the task is to check if the given string contains only special characters or not. If the string contains only special characters, then print âYesâ. Otherwise, print âNoâ. Examples: Input: str = â@#$&%!~âOutput: YesExplanation: Given string contains only special char 9 min read Program to find if a character is vowel or Consonant Given a character, check if it is vowel or consonant. Vowels are 'a', 'e', 'i', 'o' and 'u'. All other characters ('b', 'c', 'd', 'f' ....) are consonants. Examples: Input : x = 'c' Output : Consonant Input : x = 'u' Output : Vowel We check whether the given character matches any of the 5 vowels. If 12 min read Like