Check if the given string is vowel prime
Given a string str of lowercase English alphabets, the task is to check whether the string is a vowel prime or not. A string is said to be vowel prime if all the vowels in the string appear at only prime indices.
Examples:
Input: str = “geeksforgeeks”
Output: No
str[1] = ‘e’ is a vowel but 1 is not prime.
Input: str = “bcae”
Output: Yes
All the vowels are at prime indices i.e. 2 and 3.
Approach: Use Sieve of Eratosthenes to find all the prime numbers less than N so that every index of the string can be checked for primality. Now, if there is some non-prime index such that the character at that position is a vowel then the string is not vowel prime else it is.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if c is a vowel bool isVowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function that returns true if all the vowels in // the given string are only at prime indices bool isVowelPrime(string str, int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. bool prime[n]; memset(prime, true, sizeof(prime)); // 0 and 1 are not prime prime[0] = false; prime[1] = false; for (int p = 2; p * p < n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < n; i += p) prime[i] = false; } } // For every character of the given string for (int i = 0; i < n; i++) { // If current character is vowel // and the index is not prime if (isVowel(str[i]) && !prime[i]) return false; } return true; } // Driver code int main() { string str = "geeksforgeeks"; int n = str.length(); if (isVowelPrime(str, n)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true // if c is a vowel static boolean isVowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function that returns true if all the vowels in // the given string are only at prime indices static boolean isVowelPrime(String str, int n) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. // A value in prime[i] will finally be false // if i is Not a prime, else true. boolean []prime = new boolean[n]; Arrays.fill(prime, true); // 0 and 1 are not prime prime[0] = false; prime[1] = false; for (int p = 2; p * p < n; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < n; i += p) prime[i] = false; } } // For every character of the given string for (int i = 0; i < n; i++) { // If current character is vowel // and the index is not prime if (isVowel(str.charAt(i)) && !prime[i]) return false; } return true; } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; int n = str.length(); if (isVowelPrime(str, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach # Function that returns true if c is a vowel def isVowel(c): if (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'): return True return False # Function that returns true if # all the vowels in the given string # are only at prime indices def isVowelPrime(Str, n): # Create a boolean array "prime[0..n]" # and initialize all entries in it as true. # A value in prime[i] will finally be false # if i is Not a prime, else true. prime = [True for i in range(n)] # 0 and 1 are not prime prime[0] = False prime[1] = False for p in range(2, n): if p * p > n: break # If prime[p] is not changed, # then it is a prime if (prime[p] == True): # Update all multiples of p greater than or # equal to the square of it # numbers which are multiple of p and are # less than p^2 are already been marked. for i in range(2 * p, n, p): prime[i] = False # For every character of the given String for i in range(n): # If current character is vowel # and the index is not prime if (isVowel(Str[i]) and prime[i] == False): return False return True # Driver code Str= "geeksforgeeks"; n = len(Str) if (isVowelPrime(Str, n)): print("Yes") else: print("No") # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Function that returns true // if c is a vowel static Boolean isVowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true; return false; } // Function that returns true if all the vowels in // the given string are only at prime indices static Boolean isVowelPrime(String str, int n) { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. // A value in prime[i] will finally be false // if i is Not a prime, else true. Boolean []prime = new Boolean[n]; for(int i = 0; i < n; i++) prime[i] = true; // 0 and 1 are not prime prime[0] = false; prime[1] = false; for (int p = 2; p * p < n; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p greater than // or equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < n; i += p) prime[i] = false; } } // For every character of the given string for (int i = 0; i < n; i++) { // If current character is vowel // and the index is not prime if (isVowel(str[i]) && !prime[i]) return false; } return true; } // Driver code public static void Main(String[] args) { String str = "geeksforgeeks"; int n = str.Length; if (isVowelPrime(str, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Princi Singh |
No
Recommended Posts:
- Python program to check if given string is vowel Palindrome
- Check whether the frequencies of all the characters in a string are prime or not
- Check whether count of distinct characters in a string is Prime or not
- Alternate vowel and consonant string
- Lexicographically first alternate vowel and consonant string
- Count the nodes of the tree whose weighted string contains a vowel
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Check n^2 - m^2 is prime or not
- Check if N is Strong Prime
- Check if N is a Factorial Prime
- Check if a number is Full Prime
- Check whether the given numbers are Cousin prime or not
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.



