Check if the string satisfies the given condition
Last Updated :
12 Oct, 2022
Given a string str, the task is to check whether the count of vowels in the given string is prime or not. If it’s 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
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++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
bool prime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 ;
}
bool isVowel( char c)
{
c = tolower (c);
if (c == 'a'
|| c == 'e'
|| c == 'i'
|| c == 'o'
|| c == 'u' )
return true ;
return false ;
}
bool isValidString(string word)
{
int cnt = 0;
for ( int i = 0; i < word.length(); i++) {
if (isVowel(word[i]))
cnt++;
}
if (prime(cnt))
return true ;
else
return false ;
}
int main()
{
string s = "geeksforgeeks" ;
if (isValidString(s))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean prime( int n)
{
if (n <= 1 )
{
return false ;
}
if (n <= 3 )
{
return true ;
}
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 ;
}
static boolean isVowel( char c)
{
c = Character.toLowerCase(c);
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' || c == 'u' )
{
return true ;
}
return false ;
}
static boolean isValidString(String word)
{
int cnt = 0 ;
for ( int i = 0 ; i < word.length(); i++)
{
if (isVowel(word.charAt(i)))
{
cnt++;
}
}
if (prime(cnt))
{
return true ;
} else
{
return false ;
}
}
public static void main(String args[])
{
String s = "geeksforgeeks" ;
if (isValidString(s))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def prime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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
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
def isValidString(word):
cnt = 0 ;
for i in range ( len (word)):
if (isVowel(word[i])):
cnt + = 1
if (prime(cnt)):
return True
else :
return False
if __name__ = = "__main__" :
s = "geeksforgeeks"
if (isValidString(s)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static Boolean prime( int n)
{
if (n <= 1)
{
return false ;
}
if (n <= 3)
{
return true ;
}
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 ;
}
static Boolean isVowel( char c)
{
c = char .ToLower(c);
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' || c == 'u' )
{
return true ;
}
return false ;
}
static Boolean isValidString(String word)
{
int cnt = 0;
for ( int i = 0; i < word.Length; i++)
{
if (isVowel(word[i]))
{
cnt++;
}
}
if (prime(cnt))
{
return true ;
} else
{
return false ;
}
}
public static void Main(String []args)
{
String s = "geeksforgeeks" ;
if (isValidString(s))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function prime(n)
{
if (n <= 1)
{
return false ;
}
if (n <= 3)
{
return true ;
}
if (n % 2 == 0 || n % 3 == 0)
{
return false ;
}
for (let i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return false ;
}
}
return true ;
}
function isVowel(c)
{
c = c.toLowerCase();
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' || c == 'u' )
{
return true ;
}
return false ;
}
function isValidString(word)
{
let cnt = 0;
for (let i = 0; i < word.length; i++)
{
if (isVowel(word[i]))
{
cnt++;
}
}
if (prime(cnt))
{
return true ;
} else
{
return false ;
}
}
let s = "geeksforgeeks" ;
if (isValidString(s))
document.write( "YES<br>" );
else
document.write( "NO<br>" );
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...