ง่าย 2 ข้อที่แก้แล้ว อัตราผ่าน 66.7%

Count Vowels

Count the vowels in a piece of text.

สิ่งที่คุณจะได้เรียนรู้

  • Loop over the characters of a string
  • Test membership in a set of vowels
  • Ignore letter case

โจทย์

ดูฉบับแปล

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 KKKaneki Ken Python 829 ms - 5/5 ดูโค้ด
2 HJhettory Jurash Python 829 ms - 4/5 ดูโค้ด

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

สปอยล์: มีคำตอบเต็ม 2 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย