📜  检查给定的字符串是否为元音素数

📅  最后修改于: 2022-05-13 01:57:06.379000             🧑  作者: Mango

检查给定的字符串是否为元音素数

给定一个由小写英文字母组成的字符串str ,任务是检查该字符串是否是元音素数。如果字符串中的所有元音仅出现在素数索引处,则称该字符串为素数。
例子:

方法:使用埃拉托色尼筛法找出所有小于 N 的素数,以便检查字符串的每个索引是否为素数。现在,如果有一些非素数索引使得该位置的字符是元音,那么字符串不是元音素数,否则它是。
下面是上述方法的实现:

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


输出:
No