Python | Program to accept the strings which contains all vowels
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’ .
Input : geeksforgeeks Output : Not Accepted All vowels except 'a','i','u' are not present Input : ABeeIghiObhkUul Output : Accepted All vowels are present
Approach : 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.
Below is the implementation :
Time complexity : O(n)
Auxiliary Space : O(n)
Alternate Implementation :
Alternate Implementation 2.0 :
Alternate Implementation 3.0 (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.
Alternate Implementation 4.0 (using data structures):
All vowels except a,i,u are not present All vowels are present
Alternate Approach (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.
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:
The time complexity of the above code is O(n), where n is the length of the input string. This is because the code iterates through each character in the input string and performs a constant number of operations on each character.
The auxiliary space complexity of the above code is also O(n), because the code creates two sets, one to store the vowels and one to store the vowels found in the input string, both of which have a size that is equal to the length of the input string. In addition to these sets, the code also creates a list to store the count of each vowel in the input string, which also has a size equal to the length of the input string. Overall, the auxiliary space complexity is O(n).
Alternate Approach (using all () method)
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.
Approach: using the set difference() method
Please Login to comment...