Sunday, January 12, 2025
Google search engine
HomeData Modelling & AIConsonant and Vowel group check in String with Wildcard characters

Consonant and Vowel group check in String with Wildcard characters

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));


Output

0






Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
26 Oct, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments