Program to accept a Strings which contains all the Vowels
Last Updated :
14 Sep, 2023
Given a string S, the task is to check and accept the given string if it contains all vowels i.e. ‘a’, ‘e’, ‘i’.’o’, ‘u’ or ‘A’, ‘E’, ‘I’, ‘O’, ‘U’.
Examples:
Input: S = “GeeksforGeeks”
Output: Not Accepted
Since S does not contain vowels a, i and u
Input: S = “ABeeIghiObhkUul”
Output: Accepted
Since S contains all vowels a, e, i, o and u
Approach:
- Hashing can be used to solve this problem easily. For this, a hash data structure needs to be created of size 5 such that the index 0, 1, 2, 3, and 4 represent the vowels a, e, i, o, and u.
- Create a Boolean Array, as the hash data structure, to check that all vowels are present or not in the string.
- Iterate over the string character by character and if the character is vowel then mark that vowel present in the Boolean Array
- After the Iteration of the string, check that is there any vowel which is not present in the boolean array.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int checkIfAllVowels(string str)
{
int hash[5] = { 0 };
for ( int i = 0; i < str.length(); i++) {
if (str[i] == 'A' || str[i] == 'a' )
hash[0] = 1;
else if (str[i] == 'E' || str[i] == 'e' )
hash[1] = 1;
else if (str[i] == 'I' || str[i] == 'i' )
hash[2] = 1;
else if (str[i] == 'O' || str[i] == 'o' )
hash[3] = 1;
else if (str[i] == 'U' || str[i] == 'u' )
hash[4] = 1;
}
for ( int i = 0; i < 5; i++) {
if (hash[i] == 0) {
return 1;
}
}
return 0;
}
int checkIfAllVowelsArePresent(string str)
{
if (checkIfAllVowels(str))
cout << "Not Accepted\n" ;
else
cout << "Accepted\n" ;
}
int main()
{
string str = "aeioubc" ;
checkIfAllVowelsArePresent(str);
return 0;
}
|
Java
class GFG
{
static int checkIfAllVowels(String str)
{
int []hash = new int [ 5 ];
for ( int i = 0 ; i < str.length(); i++)
{
if (str.charAt(i) == 'A' || str.charAt(i) == 'a' )
hash[ 0 ] = 1 ;
else if (str.charAt(i) == 'E' || str.charAt(i) == 'e' )
hash[ 1 ] = 1 ;
else if (str.charAt(i) == 'I' || str.charAt(i) == 'i' )
hash[ 2 ] = 1 ;
else if (str.charAt(i) == 'O' || str.charAt(i) == 'o' )
hash[ 3 ] = 1 ;
else if (str.charAt(i) == 'U' || str.charAt(i) == 'u' )
hash[ 4 ] = 1 ;
}
for ( int i = 0 ; i < 5 ; i++)
{
if (hash[i] == 0 )
{
return 1 ;
}
}
return 0 ;
}
static void checkIfAllVowelsArePresent(String str)
{
if (checkIfAllVowels(str) == 1 )
System.out.print( "Not Accepted\n" );
else
System.out.print( "Accepted\n" );
}
public static void main(String[] args)
{
String str = "aeioubc" ;
checkIfAllVowelsArePresent(str);
}
}
|
Python3
def checkIfAllVowels(string) :
hash = [ 0 ] * 5 ;
for i in range ( len (string)) :
if (string[i] = = 'A' or string[i] = = 'a' ) :
hash [ 0 ] = 1 ;
elif (string[i] = = 'E' or string[i] = = 'e' ) :
hash [ 1 ] = 1 ;
elif (string[i] = = 'I' or string[i] = = 'i' ) :
hash [ 2 ] = 1 ;
elif (string[i] = = 'O' or string[i] = = 'o' ) :
hash [ 3 ] = 1 ;
elif (string[i] = = 'U' or string[i] = = 'u' ) :
hash [ 4 ] = 1 ;
for i in range ( 5 ) :
if ( hash [i] = = 0 ) :
return 1 ;
return 0 ;
def checkIfAllVowelsArePresent(string) :
if (checkIfAllVowels(string)) :
print ( "Not Accepted" );
else :
print ( "Accepted" );
if __name__ = = "__main__" :
string = "aeioubc" ;
checkIfAllVowelsArePresent(string);
|
C#
using System;
class GFG
{
static int checkIfAllVowels(String str)
{
int []hash = new int [5];
for ( int i = 0; i < str.Length; i++)
{
if (str[i] == 'A' || str[i] == 'a' )
hash[0] = 1;
else if (str[i] == 'E' || str[i] == 'e' )
hash[1] = 1;
else if (str[i] == 'I' || str[i] == 'i' )
hash[2] = 1;
else if (str[i] == 'O' || str[i] == 'o' )
hash[3] = 1;
else if (str[i] == 'U' || str[i] == 'u' )
hash[4] = 1;
}
for ( int i = 0; i < 5; i++)
{
if (hash[i] == 0)
{
return 1;
}
}
return 0;
}
static void checkIfAllVowelsArePresent(String str)
{
if (checkIfAllVowels(str) == 1)
Console.Write( "Not Accepted\n" );
else
Console.Write( "Accepted\n" );
}
public static void Main(String[] args)
{
String str = "aeioubc" ;
checkIfAllVowelsArePresent(str);
}
}
|
Javascript
<script>
function checkIfAllVowels(str)
{
var hash = Array(5).fill(0);
for ( var i = 0; i < str.length; i++) {
if (str[i] == 'A' || str[i] == 'a' )
hash[0] = 1;
else if (str[i] == 'E' || str[i] == 'e' )
hash[1] = 1;
else if (str[i] == 'I' || str[i] == 'i' )
hash[2] = 1;
else if (str[i] == 'O' || str[i] == 'o' )
hash[3] = 1;
else if (str[i] == 'U' || str[i] == 'u' )
hash[4] = 1;
}
for ( var i = 0; i < 5; i++) {
if (hash[i] == 0) {
return 1;
}
}
return 0;
}
function checkIfAllVowelsArePresent(str)
{
if (checkIfAllVowels(str))
document.write( "Not Accepted<br>" );
else
document.write( "Accepted<br>" );
}
var str = "aeioubc" ;
checkIfAllVowelsArePresent(str);
</script>
|
Time complexity: O(N), where N is the length of the given string
Auxiliary space: O(1), as constant extra space is used
Approach#2:using count() method
This code approach uses the count() method to count the occurrences of all vowels in the input string S. Then, it uses an if statement to check if the count of all vowels is greater than or equal to 1. If the condition is true, then the input string is considered to have all vowels and the output will be “Accepted”. Otherwise, the output will be “Not Accepted”.
Algorithm
- Initialize the input string S.
- Convert the input string to lowercase using the lower() method to avoid case sensitivity.
- Use the count() method to count the number of occurrences of each vowel in the input string.
- Use an if statement to check if the count of all vowels is greater than or equal to 1.
- If the condition is true, output “Accepted”. Otherwise, output “Not Accepted”.
C++
#include <algorithm>
#include <iostream>
int main()
{
std::string S = "GeeksforGeeks" ;
if (std::count(S.begin(), S.end(), 'a' ) >= 1
&& std::count(S.begin(), S.end(), 'e' ) >= 1
&& std::count(S.begin(), S.end(), 'i' ) >= 1
&& std::count(S.begin(), S.end(), 'o' ) >= 1
&& std::count(S.begin(), S.end(), 'u' ) >= 1) {
std::cout << "Accepted" << std::endl;
}
else {
std::cout << "Not Accepted" << std::endl;
}
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
String S = "GeeksforGeeks" ;
S = S.toLowerCase();
if (S.contains( "a" ) && S.contains( "e" )
&& S.contains( "i" ) && S.contains( "o" )
&& S.contains( "u" )) {
System.out.println( "Accepted" );
}
else {
System.out.println( "Not Accepted" );
}
}
}
|
Python3
S = "GeeksforGeeks"
if S.lower().count( 'a' ) > = 1 and S.lower().count( 'e' ) > = 1 and S.lower().count( 'i' ) > = 1 and S.lower().count( 'o' ) > = 1 and S.lower().count( 'u' ) > = 1 :
print ( "Accepted" )
else :
print ( "Not Accepted" )
|
C#
using System;
using System.Linq;
class GFG
{
static void Main()
{
string S = "GeeksforGeeks" ;
if (S.ToLower().Count(c => c == 'a' ) >= 1 &&
S.ToLower().Count(c => c == 'e' ) >= 1 &&
S.ToLower().Count(c => c == 'i' ) >= 1 &&
S.ToLower().Count(c => c == 'o' ) >= 1 &&
S.ToLower().Count(c => c == 'u' ) >= 1)
{
Console.WriteLine( "Accepted" );
}
else
{
Console.WriteLine( "Not Accepted" );
}
}
}
|
Javascript
let S = "GeeksforGeeks" ;
S = S.toLowerCase();
if (
S.includes( "a" ) &&
S.includes( "e" ) &&
S.includes( "i" ) &&
S.includes( "o" ) &&
S.includes( "u" )
) {
console.log( "Accepted" );
} else {
console.log( "Not Accepted" );
}
|
Time Complexity: O(n)
Auxiliary Space :O(1)
Share your thoughts in the comments
Please Login to comment...