Dada una string str de alfabetos ingleses en minúsculas, la tarea es verificar si la string es una vocal prima o no. Se dice que una string es vocal prima si todas las vocales de la string aparecen solo en índices primos.
Ejemplos:
Entrada: str = “geeksforgeeks”
Salida: No
str[1] = ‘e’ es una vocal pero 1 no es primo.
Entrada: str = «bcae»
Salida: Sí
Todas las vocales están en índices primos, es decir, 2 y 3.
Enfoque: use la criba de Eratóstenes para encontrar todos los números primos menores que N, de modo que se pueda verificar la primalidad de todos los índices de la string. Ahora, si hay algún índice no primo tal que el carácter en esa posición es una vocal, entonces la string no es una vocal prima sino lo es.
A continuación se muestra la implementación del enfoque anterior:
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
Javascript
<script> // Javascript implementation of the approach // Function that returns true if c is a vowel function isVowel(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 function isVowelPrime(str, 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. var prime = Array(n).fill(true); // 0 and 1 are not prime prime[0] = false; prime[1] = false; for (var 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 (var i = p * p; i < n; i += p) prime[i] = false; } } // For every character of the given string for (var 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 var str = "geeksforgeeks"; var n = str.length; if (isVowelPrime(str, n)) document.write( "Yes"); else document.write( "No"); </script>
No