Open In App

Program to accept a Strings which contains all the Vowels

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: 

Below is the implementation of the above approach:




// C++ implementation to check that
// a string contains all vowels
 
#include <iostream>
using namespace std;
 
// Function to check that
// a string contains all vowels
int checkIfAllVowels(string str)
{
 
    // Hash Array of size 5
    // such that the index 0, 1, 2, 3 and 4
    // represent the vowels a, e, i, o and u
    int hash[5] = { 0 };
 
    // Loop the string to mark the vowels
    // which are present
    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;
    }
 
    // Loop to check if there is any vowel
    // which is not present in the string
    for (int i = 0; i < 5; i++) {
        if (hash[i] == 0) {
            return 1;
        }
    }
    return 0;
}
 
// Function to check that
// a string contains all vowels
int checkIfAllVowelsArePresent(string str)
{
 
    if (checkIfAllVowels(str))
        cout << "Not Accepted\n";
    else
        cout << "Accepted\n";
}
 
// Driver Code
int main()
{
    string str = "aeioubc";
    checkIfAllVowelsArePresent(str);
 
    return 0;
}




// Java implementation to check that
// a String contains all vowels
 
class GFG
{
 
// Function to check that
// a String contains all vowels
static int checkIfAllVowels(String str)
{
 
    // Hash Array of size 5
    // such that the index 0, 1, 2, 3 and 4
    // represent the vowels a, e, i, o and u
    int []hash = new int[5];
 
    // Loop the String to mark the vowels
    // which are present
    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;
    }
 
    // Loop to check if there is any vowel
    // which is not present in the String
    for (int i = 0; i < 5; i++)
    {
        if (hash[i] == 0)
        {
            return 1;
        }
    }
return 0;
}
 
// Function to check that
// a String contains all vowels
static void checkIfAllVowelsArePresent(String str)
{
 
    if (checkIfAllVowels(str) == 1)
        System.out.print("Not Accepted\n");
    else
        System.out.print("Accepted\n");
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "aeioubc";
    checkIfAllVowelsArePresent(str);
}
}
 
// This code is contributed by 29AjayKumar




# Python3 implementation to check that
# a string contains all vowels
 
# Function to check that
# a string contains all vowels
def checkIfAllVowels(string) :
 
    # Hash Array of size 5
    # such that the index 0, 1, 2, 3 and 4
    # represent the vowels a, e, i, o and u
    hash = [0]*5 ;
     
    # Loop the string to mark the vowels
    # which are present
    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;
             
    # Loop to check if there is any vowel
    # which is not present in the string
    for i in range(5) :
        if (hash[i] == 0) :
            return 1;
             
    return 0;
 
# Function to check that
# a string contains all vowels
def checkIfAllVowelsArePresent(string) :
 
    if (checkIfAllVowels(string)) :
        print("Not Accepted");
    else :
        print("Accepted");
 
# Driver Code
if __name__ == "__main__" :
 
    string = "aeioubc";
    checkIfAllVowelsArePresent(string);
 
# This code is contributed by AnkitRai01




// C# implementation to check that
// a String contains all vowels
using System;
 
class GFG
{
 
// Function to check that
// a String contains all vowels
static int checkIfAllVowels(String str)
{
 
    // Hash Array of size 5
    // such that the index 0, 1, 2, 3 and 4
    // represent the vowels a, e, i, o and u
    int []hash = new int[5];
 
    // Loop the String to mark the vowels
    // which are present
    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;
    }
 
    // Loop to check if there is any vowel
    // which is not present in the String
    for (int i = 0; i < 5; i++)
    {
        if (hash[i] == 0)
        {
            return 1;
        }
    }
return 0;
}
 
// Function to check that
// a String contains all vowels
static void checkIfAllVowelsArePresent(String str)
{
 
    if (checkIfAllVowels(str) == 1)
        Console.Write("Not Accepted\n");
    else
        Console.Write("Accepted\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    String str = "aeioubc";
    checkIfAllVowelsArePresent(str);
}
}
 
// This code is contributed by 29AjayKumar




<script>
 
// JavaScript implementation to check that
// a string contains all vowels
 
// Function to check that
// a string contains all vowels
function checkIfAllVowels(str)
{
 
    // Hash Array of size 5
    // such that the index 0, 1, 2, 3 and 4
    // represent the vowels a, e, i, o and u
    var hash = Array(5).fill(0);
 
    // Loop the string to mark the vowels
    // which are present
    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;
    }
 
    // Loop to check if there is any vowel
    // which is not present in the string
    for (var i = 0; i < 5; i++) {
        if (hash[i] == 0) {
            return 1;
        }
    }
    return 0;
}
 
// Function to check that
// a string contains all vowels
function checkIfAllVowelsArePresent(str)
{
 
    if (checkIfAllVowels(str))
        document.write( "Not Accepted<br>");
    else
        document.write( "Accepted<br>");
}
 
// Driver Code
var str = "aeioubc";
checkIfAllVowelsArePresent(str);   
 
</script>

Output
Accepted







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




#include <algorithm>
#include <iostream>
 
int main()
{
    std::string S = "GeeksforGeeks";
 
    // Check if S contains all vowels using count() and if
    // statement Convert the string to lowercase to make the
    // comparison case-insensitive
    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) {
        // All vowels are present in the string
        std::cout << "Accepted" << std::endl;
    }
    else {
        // At least one of the vowels is missing in the
        // string
        std::cout << "Not Accepted" << std::endl;
    }
 
    return 0;
}




import java.util.*;
 
public class GFG {
 
    public static void main(String[] args)
    {
        String S = "GeeksforGeeks";
 
        // Convert the string to lowercase to make the
        // comparison case-insensitive
        S = S.toLowerCase();
 
        // Check if S contains all vowels using the
        // contains() method and if statement
        if (S.contains("a") && S.contains("e")
            && S.contains("i") && S.contains("o")
            && S.contains("u")) {
            // All vowels are present in the string
            System.out.println("Accepted");
        }
        else {
            // At least one of the vowels is missing in the
            // string
            System.out.println("Not Accepted");
        }
    }
}




S = "GeeksforGeeks"
 
# Check if S contains all vowels using count() and if statement
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")




using System;
using System.Linq;
 
class GFG
{
    static void Main()
    {
        string S = "GeeksforGeeks";
 
        // Check if S contains all vowels using count() and
       // if statement
        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 implementation of checking if a string contains all vowels
 
// Given string
let S = "GeeksforGeeks";
 
// Convert the string to lowercase to make the comparison case-insensitive
S = S.toLowerCase();
 
// Check if S contains all vowels using the includes() method and if statement
if (
  S.includes("a") &&
  S.includes("e") &&
  S.includes("i") &&
  S.includes("o") &&
  S.includes("u")
) {
  // All vowels are present in the string
  console.log("Accepted");
} else {
  // At least one of the vowels is missing in the string
  console.log("Not Accepted");
}
 
// This Code is Contributed by Shivam Tiwari

Output
Not Accepted







Time Complexity: O(n)
Auxiliary Space :O(1)


Article Tags :