Check if the string satisfies the given condition

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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 // C++ implementation of the approach #include #define ll long long int using namespace std;    // Function that returns true if n is prime bool 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 bool 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 prime bool 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 int main() {     string s = "geeksforgeeks";     if (isValidString(s))         cout << "YES";     else         cout << "NO";     return 0; }

Java

 // Java implementation of the approach import 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

C#

 // C# implementation of the approach using 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

Output:

YES

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : princi singh, 29AjayKumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.