📌  相关文章
📜  检查字符串是否满足给定条件

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

检查字符串是否满足给定条件

给定一个字符串str ,任务是检查给定字符串中元音的数量是否为素数。如果它是素数,则打印YES否则打印NO

例子:

方法:初始化字符变量count = 0并逐个字符遍历字符串,如果当前字符是元音则更新count = count + 1 。最后,如果count是素数,则打印YES否则打印NO

下面是上述方法的实现:

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


Python3
# Python3 implementation of the approach
  
# Function that returns true if n is prime
def 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 vowel
def 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 prime
def 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 code
if __name__=="__main__":
     
    s = "geeksforgeeks"
     
    if (isValidString(s)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by rutvik_56


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


Javascript


输出:
YES