簡単 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 つずつ開いてみましょう。

ヒント 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レビューも見ることができます。 サインインして課題を解くと議論に参加できます。