Đề bài
Count how many characters in text are vowels: a, e, i, o, u in either uppercase or lowercase.
Implement count_vowels(text).
Input
text: The text to inspect (ASCII characters).
Output
Return the number of vowels.
Ràng buộc
0 ≤ length ≤ 10,000
Ví dụ
Đầu vào:
text = "Hello World"
Kết quả mong đợi:
expected = 3
Đầu vào:
std::string text = "Hello World";
Kết quả mong đợi:
long long expected = 3;
Đầu vào:
string text = "Hello World";
Kết quả mong đợi:
long expected = 3;
Giải thích
e, o and o are vowels: 3.
Đầu vào:
text = "AEIOU and y"
Kết quả mong đợi:
expected = 6
Đầu vào:
std::string text = "AEIOU and y";
Kết quả mong đợi:
long long expected = 6;
Đầu vào:
string text = "AEIOU and y";
Kết quả mong đợi:
long expected = 6;
Giải thích
A, E, I, O, U, a are vowels; y is not counted: 6.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Scan the text once and test each character against the vowel set.
Gợi ý 2
Treat uppercase and lowercase vowels as the same kind of letter.
Gợi ý 3
An empty string should simply return zero.
Độ phức tạp mong đợi
O(n) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.