Open In App

Python Program to Accept the Strings Which Contains all Vowels

Last Updated : 30 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python program to accept the strings
# which contains all the vowels
 
# Function for check if string
# is accepted or not
def check(string) :
 
    string = string.lower()
 
    # set() function convert "aeiou"
    # string into set of characters
    # i.e.vowels = {'a', 'e', 'i', 'o', 'u'}
    vowels = set("aeiou")
 
    # set() function convert empty
    # dictionary into empty set
    s = set({})
 
    # looping through each
    # character of the string
    for char in string :
 
        # Check for the character is present inside
        # the vowels set or not. If present, then
        # add into the set s by using add method
        if char in vowels :
            s.add(char)
        else:
            pass
             
    # check the length of set s equal to length
    # of vowels set or not. If equal, string is 
    # accepted otherwise not
    if len(s) == len(vowels) :
        print("Accepted")
    else :
        print("Not Accepted")
 
 
# Driver code
if __name__ == "__main__" :
     
    string = "SEEquoiaL"
 
    # calling function
    check(string)


Output

Accepted

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 0 is present int vowel count array
    if vowel.count(0) > 0:
        return('not accepted')
    else:
        return('accepted')
 
 
# Driver code
if __name__ == "__main__":
 
    string = "SEEquoiaL"
 
    print(check(string))


Output

accepted

Alternate Implementation:

Python3




# Python program for the above approach
def check(string):
    if len(set(string.lower()).intersection("aeiou")) >= 5:
        return ('accepted')
    else:
        return ("not accepted")
 
 
# Driver code
if __name__ == "__main__":
    string = "geeksforgeeks"
    print(check(string))


Output

not accepted

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 library
import re
 
sampleInput = "aeioAEiuioea"
 
# regular expression to find the strings
# which have characters other than a,e,i,o and u
c = re.compile('[^aeiouAEIOU]')
 
# use findall() to get the list of strings
# that have characters other than a,e,i,o and u.
if(len(c.findall(sampleInput))):
    print("Not Accepted"# if length of list > 0 then it is not accepted
else:
    print("Accepted"# if length of list = 0 then it is accepted


Output

Accepted

Accept the Strings Which Contains all Vowelsusing data structures

Python3




# Python | Program to accept the strings which contains all vowels
 
 
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"
 
 
# function-call
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




# Python program to accept the strings
# which contains all the vowels
 
# Function for check if string
# is accepted or not
def check(string) :
     
    # Checking if "aeiou" is a subset of the set of all letters in the string
    if set("aeiou").issubset(set(string.lower())):
      return "Accepted"
    return "Not accepted"
 
 
# Driver code
if __name__ == "__main__" :
     
    string = "SEEquoiaL"
 
    # calling function
    print(check(string))


Output

Accepted

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):
    # create a Counter object to count the occurrences of each character
    counter = collections.Counter(string.lower())
     
    # set of vowels
    vowels = set("aeiou")
     
    # counter for the number of vowels present
    vowel_count = 0
     
    # check if each vowel is present in the string
    for vowel in vowels:
        if counter[vowel] > 0:
            vowel_count += 1
     
    # check if all vowels are present
    if vowel_count == len(vowels):
        print("Accepted")
    else:
        print("Not Accepted")
 
# test the function
string = "SEEquoiaL"
check(string)


Output

Accepted

Accept the Strings Which Contains all Vowels using  all () method

Python3




# Python program to accept the strings
# which contains all the vowels
 
# Function for check if string
# is accepted or not
 
#using all() method
 
def check(string):
    vowels = "aeiou" #storing vowels
    if all(vowel in string.lower() for vowel in vowels):
        return "Accepted"
    return "Not accepted"
 
 
 
#initializing string
string = "SEEquoiaL"
# test the function
print(check(string))
 
#this code contributed by tvsk


Output

Accepted

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




#function definition
def check(s):
  A = {'a', 'e', 'i', 'o', 'u'}
  #using the set difference
  if len(A.difference(set(s.lower())))==0:
    print('accepted')
  else:
    print('not accepted')
#input
s='SEEquoiaL'
#function call
check(s)


Output

accepted

Time complexity:O(n)
Auxiliary Space:O(n)



Similar Reads

Modify the string such that it contains all vowels at least once
Given a string S containing only Uppercase letters, the task is to find the minimum number of replacement of characters needed to get a string with all vowels and if we cannot make the required string then print Impossible. Examples: Input: str = "ABCDEFGHI"Output: AOUDEFGHIExplanation: There are already 3 Vowels present in the string A, E, I we ju
12 min read
Length of the longest substring that contains even number of vowels
Given a string S consisting of N lowercase characters, the task is to find the length of the longest substring consisting of each vowel an even number of times Examples: Input: S= "bcbcbc"Output: 6Explanation:Consider the substring S[0, 5] i.e., "bcbcbc" is the longest substring because all vowels: a, e, i, o and u appear 0(which is even) number of
8 min read
Python Regex - Program to accept string starting with vowel
Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is starting with Vowel or Not.Examples: Input: animal Output: Accepted Input: zebra Output: Not Accepted In this program, we are using search() method of re module.re.search() : This method either returns None (if the pattern doesn’t m
4 min read
Python Regex | Program to accept string ending with alphanumeric character
Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is ending with only alphanumeric character or Not.Examples: Input: ankitrai326 Output: Accept Input: ankirai@ Output: Discard In this program, we are using search() method of re module. re.search() : This method either returns None (if
2 min read
Python program to count number of vowels using sets in given string
Given a string, count the number of vowels present in the given string using Sets. You can use sets to count the number of vowels in a given string in Python. Sets are useful to efficiently check for counting the unique occurrences of vowels. Input : GeeksforGeeksOutput : No. of vowels : 5Explaination: The string GeeksforGeeks contains 5 vowels in
4 min read
Python Program to print element with maximum vowels from a List
Given a list containing string elements, the task is to write a Python program to print a string with maximum vowels. Input : test_list = ["gfg", "best", "for", "geeks"] Output : geeks Explanation : geeks has 2 e's which is a maximum number of vowels compared to other strings.Input : test_list = ["gfg", "best"] Output : best Explanation : best has
7 min read
Python Program to Count characters surrounding vowels
Given a String, the task is to write a Python program to count those characters which have vowels as their neighbors. Examples: Input : test_str = 'geeksforgeeksforgeeks' Output : 10 Explanation : g, k, f, r, g, k, f, r, g, k have surrounding vowels. Input : test_str = 'geeks' Output : 2 Explanation : g, k have surrounding vowels. Method 1 : Using
3 min read
Python Program to Count Vowels, Lines, Characters in Text File
In this article, we are going to create a python program that counts vowels, lines, and a number of characters present in a particular text file. ApproachWe have to open the file using open() function in python.Then make three variables, vowel, line and character to count the number of vowels, lines, and characters respectively.Make a list of vowel
2 min read
Python Program to Count the Number of Vowels in a String
In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first design a simple approach that we will be following in
10 min read
Python Program to Count Vowels in a String Using Recursion
Given a string, our task is to count the number of vowels in the string using recursion in Python and return the number of vowels. Examples: Input: acbedOutput: 2Input: geeksforgeeksOutput: 5Explanation: We are counting vowels in a string and printing the number of vowels.Python Program to Count Vowels in a String Using RecursionWe can use a recurs
3 min read