Python Program to Accept the Strings Which Contains all Vowels
Last Updated :
30 Jun, 2023
Given a string, the task is to check if every vowel is present or not. We consider a vowel to be present if it is present in upper case or lower case. i.e. ‘a’, ‘e’, ‘i’.’o’, ‘u’ or ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ .
Examples :
Input : geeksforgeeks
Output : Not Accepted
All vowels except 'a','i','u' are not present
Input : ABeeIghiObhkUul
Output : Accepted
All vowels are present
Python Program to Accept the Strings Which Contains all Vowels
Firstly, create set of vowels using set() function. Check for each character of the string is vowel or not, if vowel then add into the set s. After coming out of the loop, check length of the set s, if length of set s is equal to the length of the vowels set then string is accepted otherwise not.
Python3
def check(string) :
string = string.lower()
vowels = set ( "aeiou" )
s = set ({})
for char in string :
if char in vowels :
s.add(char)
else :
pass
if len (s) = = len (vowels) :
print ( "Accepted" )
else :
print ( "Not Accepted" )
if __name__ = = "__main__" :
string = "SEEquoiaL"
check(string)
|
Time complexity : O(n)
Auxiliary Space : O(n)
Strings Which Contains all Vowels Using builtin
Python3
def check(string):
string = string.replace( ' ' , '')
string = string.lower()
vowel = [string.count( 'a' ), string.count( 'e' ), string.count(
'i' ), string.count( 'o' ), string.count( 'u' )]
if vowel.count( 0 ) > 0 :
return ( 'not accepted' )
else :
return ( 'accepted' )
if __name__ = = "__main__" :
string = "SEEquoiaL"
print (check(string))
|
Alternate Implementation:
Python3
def check(string):
if len ( set (string.lower()).intersection( "aeiou" )) > = 5 :
return ( 'accepted' )
else :
return ( "not accepted" )
if __name__ = = "__main__" :
string = "geeksforgeeks"
print (check(string))
|
Accept the Strings Which Contains all Vowels Using Regular Expressions
Compile a regular expression using compile() for “character is not a, e, i, o and u”. Use re.findall() to fetch the strings satisfying the above regular expression. Print output based on the result.
Python3
import re
sampleInput = "aeioAEiuioea"
c = re. compile ( '[^aeiouAEIOU]' )
if ( len (c.findall(sampleInput))):
print ( "Not Accepted" )
else :
print ( "Accepted" )
|
Accept the Strings Which Contains all Vowelsusing data structures
Python3
def all_vowels(str_value):
new_list = [char for char in str_value.lower() if char in 'aeiou' ]
if new_list:
dic, lst = {}, []
for char in new_list:
dic[ 'a' ] = new_list.count( 'a' )
dic[ 'e' ] = new_list.count( 'e' )
dic[ 'i' ] = new_list.count( 'i' )
dic[ 'o' ] = new_list.count( 'o' )
dic[ 'u' ] = new_list.count( 'u' )
for i, j in dic.items():
if j = = 0 :
lst.append(i)
if lst:
return f "All vowels except {','.join(lst)} are not present"
else :
return 'All vowels are present'
else :
return "No vowels present"
str_value = "geeksforgeeks"
print (all_vowels(str_value))
str_value = "ABeeIghiObhkUul"
print (all_vowels(str_value))
|
Output
All vowels except a,i,u are not present
All vowels are present
Accept the Strings Which Contains all Vowels using set methods
The issubset() attribute of a set in Python3 checks if all the elements of a given set are present in another set.
Python3
def check(string) :
if set ( "aeiou" ).issubset( set (string.lower())):
return "Accepted"
return "Not accepted"
if __name__ = = "__main__" :
string = "SEEquoiaL"
print (check(string))
|
Accept the Strings Which Contains all Vowels Using collections
One approach using the collections module could be to use a Counter object to count the occurrences of each character in the string. Then, we can check if the count for each vowel is greater than 0. If it is, we can add 1 to a counter variable. At the end, we can check if the counter variable is equal to the number of vowels. If it is, the string is accepted, otherwise it is not accepted.
Here is an example of this approach:
Python3
import collections
def check(string):
counter = collections.Counter(string.lower())
vowels = set ( "aeiou" )
vowel_count = 0
for vowel in vowels:
if counter[vowel] > 0 :
vowel_count + = 1
if vowel_count = = len (vowels):
print ( "Accepted" )
else :
print ( "Not Accepted" )
string = "SEEquoiaL"
check(string)
|
Accept the Strings Which Contains all Vowels using all () method
Python3
def check(string):
vowels = "aeiou"
if all (vowel in string.lower() for vowel in vowels):
return "Accepted"
return "Not accepted"
string = "SEEquoiaL"
print (check(string))
|
The Time Complexity of the function is O(n), and the Space Complexity is O (1) which makes this function efficient and suitable for most use cases.
Accept the Strings Which Contains all Vowels using the set difference() method
Python3
def check(s):
A = { 'a' , 'e' , 'i' , 'o' , 'u' }
if len (A.difference( set (s.lower()))) = = 0 :
print ( 'accepted' )
else :
print ( 'not accepted' )
s = 'SEEquoiaL'
check(s)
|
Time complexity:O(n)
Auxiliary Space:O(n)
Please Login to comment...