Given a string consisting of English alphabetic characters (both uppercase and lowercase), count the number of distinct vowels present. Vowels are 'a', 'e', 'i', 'o', 'u', and the count is case-insensitive (i.e., 'A' and 'a' are considered the same vowel).
Input: "Hello"
Output: 2
Explanation: The string "Hello" contains vowels 'e' and 'o', which are distinct, so the count is 2.
Input: "AEiou"
Output: 5
Explanation: All five vowels ('a', 'e', 'i', 'o', 'u') are present, and since the count is case-insensitive, they are all distinct, resulting in a count of 5.
Input: "bcdfg"
Output: 0
Explanation: The string contains no vowels, so the count of distinct vowels is 0.
We need to count distinct vowels case-insensitively. Using a set automatically handles uniqueness, and converting each character to lowercase ensures 'A' and 'a' are treated as the same vowel.
#include <iostream>
#include <unordered_set>
#include <cctype>
using namespace std;
int main() {
string s;
cin >> s;
unordered_set<char> distinctVowels;
for (char c : s) {
c = tolower(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
distinctVowels.insert(c);
}
}
cout << distinctVowels.size() << endl;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
Set<Character> distinctVowels = new HashSet<>();
for (char c : s.toCharArray()) {
c = Character.toLowerCase(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
distinctVowels.add(c);
}
}
System.out.println(distinctVowels.size());
}
}
s = input().strip()
vowels = set()
for c in s:
c = c.lower()
if c in ['a','e','i','o','u']:
vowels.add(c)
print(len(vowels))
const s = require('fs').readFileSync(0, 'utf8').trim();
let distinctVowels = new Set();
for (let c of s) {
c = c.toLowerCase();
if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u') {
distinctVowels.add(c);
}
}
console.log(distinctVowels.size);
Time Complexity: O(n), where n is the string length, as we process each character once and set operations are O(1) average.
Space Complexity: O(1), since the set stores at most 5 vowels (constant space).
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.