โจทย์
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.
เงื่อนไข
0 ≤ length ≤ 10,000
ตัวอย่าง
ตัวอย่างที่ 1
อินพุต:
text = "Hello World"
ผลลัพธ์ที่คาดหวัง:
expected = 3
อินพุต:
std::string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
long long expected = 3;
อินพุต:
string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
long expected = 3;
คำอธิบาย
e, o and o are vowels: 3.
ตัวอย่างที่ 2
อินพุต:
text = "AEIOU and y"
ผลลัพธ์ที่คาดหวัง:
expected = 6
อินพุต:
std::string text = "AEIOU and y";
ผลลัพธ์ที่คาดหวัง:
long long expected = 6;
อินพุต:
string text = "AEIOU and y";
ผลลัพธ์ที่คาดหวัง:
long expected = 6;
คำอธิบาย
A, E, I, O, U, a are vowels; y is not counted: 6.
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Scan the text once and test each character against the vowel set.
คำใบ้ 2
Treat uppercase and lowercase vowels as the same kind of letter.
คำใบ้ 3
An empty string should simply return zero.
ความซับซ้อนที่คาดหวัง
O(n) time, O(1) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน