Check if the string satisfies the given condition - GeeksforGeeks

29 Jul, 2021
Given a string str, the task is to check whether the count of vowels in the given string is prime or not. If its prime then print YES else print NO.
Examples: 
Input: str = “geeksforgeeks” Output: YES Count of vowels is 5 (e, e, o, e and e) which is prime.
Input: str = “aeioua” Output: NO 
Approach: Initialize a variable count = 0 and traverse the string character by character, if the current character is a vowel then update count = count + 1. In the end, if count is prime then print YES else print NO.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>#define ll long long intusing namespace std; // Function that returns true if n is primebool prime(int n){    // Corner cases    if (n <= 1)        return false;    if (n <= 3)        return true;     // This is checked so that we can skip    // middle five numbers in below loop    if (n % 2 == 0 || n % 3 == 0)        return false;     for (int i = 5; i * i <= n; i = i + 6)        if (n % i == 0 || n % (i + 2) == 0)            return false;     return true;} // Function that returns true if c is a vowelbool isVowel(char c){    c = tolower(c);    if (c == 'a'        || c == 'e'        || c == 'i'        || c == 'o'        || c == 'u')        return true;    return false;} // Function that returns true if the count// of vowels in word is primebool isValidString(string word){    int cnt = 0;     for (int i = 0; i < word.length(); i++) {        if (isVowel(word[i]))            cnt++;    }     // If count of vowels is prime    if (prime(cnt))        return true;    else        return false;} // Driver codeint main(){    string s = "geeksforgeeks";    if (isValidString(s))        cout << "YES";    else        cout << "NO";    return 0;}
// Java implementation of the approachimport java.util.*; class GFG{     // Function that returns true if n is prime    static boolean prime(int n)    {        // Corner cases        if (n <= 1)        {            return false;        }        if (n <= 3)        {            return true;        }         // This is checked so that we can skip        // middle five numbers in below loop        if (n % 2 == 0 || n % 3 == 0)        {            return false;        }         for (int i = 5; i * i <= n; i = i + 6)        {            if (n % i == 0 || n % (i + 2) == 0)            {                return false;            }        }         return true;    }     // Function that returns true if c is a vowel    static boolean isVowel(char c)    {        c = Character.toLowerCase(c);        if (c == 'a' || c == 'e' ||            c == 'i' || c == 'o' || c == 'u')        {            return true;        }        return false;    }     // Function that returns true if the count    // of vowels in word is prime    static boolean isValidString(String word)    {        int cnt = 0;         for (int i = 0; i < word.length(); i++)        {            if (isVowel(word.charAt(i)))            {                cnt++;            }        }         // If count of vowels is prime        if (prime(cnt))        {            return true;        } else        {            return false;        }    }     // Driver code    public static void main(String args[])    {        String s = "geeksforgeeks";        if (isValidString(s))            System.out.println("YES");        else            System.out.println("NO");    }} // This code is contributed by Princi Singh
# Python3 implementation of the approach  # Function that returns true if n is primedef prime(n):     # Corner cases    if (n <= 1):        return False    if (n <= 3):        return True      # This is checked so that we can skip    # middle five numbers in below loop    if (n % 2 == 0 or n % 3 == 0):        return False         i = 5    while i * i <= n:        if (n % i == 0 or n % (i + 2) == 0):            return False                     i += 6     return True     # Function that returns true# if c is a voweldef isVowel(c):     c = c.lower()    if (c == 'a' or c == 'e' or        c == 'i' or c == 'o' or        c == 'u'):        return True             return False # Function that returns true if the# count of vowels in word is primedef isValidString(word):         cnt = 0;         for i in range(len(word)):        if (isVowel(word[i])):            cnt += 1      # If count of vowels is prime    if (prime(cnt)):        return True    else:        return False # Driver codeif __name__=="__main__":         s = "geeksforgeeks"         if (isValidString(s)):        print("YES")    else:        print("NO") # This code is contributed by rutvik_56
// C# implementation of the approachusing System;     class GFG{     // Function that returns true if n is prime    static Boolean prime(int n)    {        // Corner cases        if (n <= 1)        {            return false;        }        if (n <= 3)        {            return true;        }         // This is checked so that we can skip        // middle five numbers in below loop        if (n % 2 == 0 || n % 3 == 0)        {            return false;        }         for (int i = 5; i * i <= n; i = i + 6)        {            if (n % i == 0 || n % (i + 2) == 0)            {                return false;            }        }         return true;    }     // Function that returns true if c is a vowel    static Boolean isVowel(char c)    {        c = char.ToLower(c);        if (c == 'a' || c == 'e' ||            c == 'i' || c == 'o' || c == 'u')        {            return true;        }        return false;    }     // Function that returns true if the count    // of vowels in word is prime    static Boolean isValidString(String word)    {        int cnt = 0;         for (int i = 0; i < word.Length; i++)        {            if (isVowel(word[i]))            {                cnt++;            }        }         // If count of vowels is prime        if (prime(cnt))        {            return true;        } else        {            return false;        }    }     // Driver code    public static void Main(String []args)    {        String s = "geeksforgeeks";        if (isValidString(s))            Console.WriteLine("YES");        else            Console.WriteLine("NO");    }} // This code has been contributed by 29AjayKumar
<script> // JavaScript implementation of the approach // Function that returns true if n is primefunction prime(n){    // Corner cases        if (n <= 1)        {            return false;        }        if (n <= 3)        {            return true;        }          // This is checked so that we can skip        // middle five numbers in below loop        if (n % 2 == 0 || n % 3 == 0)        {            return false;        }          for (let i = 5; i * i <= n; i = i + 6)        {            if (n % i == 0 || n % (i + 2) == 0)            {                return false;            }        }          return true;} // Function that returns true if c is a vowelfunction isVowel(c){    c = c.toLowerCase();        if (c == 'a' || c == 'e' ||            c == 'i' || c == 'o' || c == 'u')        {            return true;        }        return false;}  // Function that returns true if the count    // of vowels in word is primefunction isValidString(word){    let cnt = 0;          for (let i = 0; i < word.length; i++)        {            if (isVowel(word[i]))            {                cnt++;            }        }          // If count of vowels is prime        if (prime(cnt))        {            return true;        } else        {            return false;        }} // Driver codelet s = "geeksforgeeks";if (isValidString(s))    document.write("YES<br>");else    document.write("NO<br>");  // This code is contributed by rag2127 </script>
YES
 
princi singh
29AjayKumar
rutvik_56
rag2127
Prime Number
vowel-consonant
Strings
Strings
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Print all the duplicates in the input string
Vigenère Cipher
String class in Java | Set 1
sprintf() in C
Print all subsequences of a string
Convert character array to string in C++
How to Append a Character to a String in C
Program to count occurrence of a given character in a string
Naive algorithm for Pattern Searching