Consonant and Vowel group check in String with Wildcard characters
Last Updated :
26 Oct, 2023
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++
#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 ;
}
int main()
{
string s = "bcdaeiou??";
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));
}
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)
{
int v = 0;
int c = 0;
for ( int i = 0; i < S.Length; i++)
{
char ch = S[i];
if (Vowel(ch))
{
c = 0;
v += 1;
}
else if (ch == '?' )
{
v += 1;
c += 1;
}
else
{
v = 0;
c += 1;
}
if (v > 5 || c > 3)
{
return "String is Bad" ;
}
}
return "String is Good" ;
}
}
|
Javascript
function vowel(c) {
if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' ) {
return true ;
}
return false ;
}
function isValid(S) {
let v = 0, c = 0;
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 (v > 5 || c > 3) {
return "String is Bad" ;
}
}
return "String is Good" ;
}
let s = "bcdaeiou??" ;
console.log(isValid(s));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...