Python program to count number of vowels using sets in given string
Given a string, count the number of vowels present in given string using Sets. Prerequisite: Sets in Python Examples:
Input : GeeksforGeeks Output : No. of vowels : 5 Input : Hello World Output : No. of vowels : 3
Approach: 1. Create a set of vowels using set() and initialize a count variable to 0. 2. Traverse through the alphabets in the string and check if the letter in the string is present in set vowel. 3. If it is present, the vowel count is incremented. Below is the implementation of above approach:
No. of vowels : 5
Time Complexity: O(n), where n is the length of the string. We traverse the string character by character and for each character, we do a constant time operation to check if it is a vowel or not.
Space Complexity: O(1). We only used constant extra space for setting up the set.