The task is to check whether a given string contains all the vowels a, e, i, o, u. For example:
Input: "education"
Output: TrueInput: "geeksforgeeks"
Output: False
Using set()
set() function is used to store unique elements and can efficiently check if all vowels are present in a string.
s = "geeksforgeeks"
v = set('aeiou')
if v.issubset(set(s.lower())):
print("True")
else:
print("False")
Output
False
Explanation:
- set('aeiou') creates a set of vowels.
- set(s.lower()) converts the string to lowercase and stores unique characters.
- issubset() checks if all vowels are present in the string.
Using all()
all() function checks if all vowels are present in the string. It returns True if every condition in the list comprehension is met.
s = "Geeksforgeeks"
v = 'aeiou'
if all(i in s.lower() for i in v):
print("True")
else:
print("False")
Output
False
Explanation:
- s.lower() ensures the check is case-insensitive.
- all() returns True only if every vowel in v is found in the string.
- Generator expression (ch in s.lower() for ch in v) efficiently iterates through vowels.
Using a Loop
Looping through the string, this method checks for the presence of each vowel. It stops early once all vowels are found, ensuring an efficient solution with linear time complexity.
s = "education"
v = 'aeiou'
a = set()
for char in s.lower():
if char in v:
a.add(char)
if len(a) == 5:
print("True")
break
else:
print("False")
Output
True
Explanation:
- found stores vowels detected in the string.
- Loop iterates over each character and adds it to found if it’s a vowel.
- Breaks early if all five vowels are found; otherwise prints False after loop.
Using Regular Expression
Regular expressions allow checking for all vowels regardless of order. This method is less efficient for very long strings but concise.
import re
s = "geeksforgeeks"
if re.search(r'(?=.*a)(?=.*e)(?=.*i)(?=.*o)(?=.*u)', s.lower()):
print("True")
else:
print("False")
Output
False
Explanation:
- (?=.*a) ensures that a exists somewhere in the string.
- Combined regex ensures all vowels are present.
- re.search() returns a match if all conditions are satisfied.