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:
- 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++
// C++ implementation to check that // a string contains all vowels #include <iostream> using namespace std; // Function to 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 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
// Java implementation to check that // a String contains all vowels class GFG { // Function to 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 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
# Python3 implementation to check that # a string contains all vowels # Function to 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 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#
// C# implementation to check that // a String contains all vowels using System; class GFG { // Function to 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 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 |
Javascript
<script> // JavaScript implementation to check that // a string contains all vowels // Function to 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 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