簡単 8件解決済み 正答率100.0%

Count Uppercase Letters

Count the capital letters in a text.

このチャレンジで学べること

  • Loop over the characters of a string
  • Detect uppercase letters
  • Count matches

問題

翻訳を表示

Count how many characters in text are uppercase English letters A–Z.

Implement count_uppercase(text).

Input

text: The text to inspect.

Output

Return the number of uppercase letters.

制約

0 ≤ length of text ≤ 10,000 (ASCII characters)

例

例 1
入力:
text = "Hello World"

期待される出力:
expected = 2
入力:
std::string text = "Hello World";

期待される出力:
long long expected = 2;
入力:
string text = "Hello World";

期待される出力:
long expected = 2;

解説

H and W are uppercase: 2.

例 2
入力:
text = "no caps here!"

期待される出力:
expected = 0
入力:
std::string text = "no caps here!";

期待される出力:
long long expected = 0;
入力:
string text = "no caps here!";

期待される出力:
long expected = 0;

解説

There are no uppercase letters: 0.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Scan the text once and notice only letters A through Z count.

ヒント 2

Check each character’s case directly instead of comparing against words or splitting the string.

ヒント 3

Remember lowercase letters, digits, spaces, and punctuation do not contribute.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 Ccheodo Python 797 ms - 3/5 コードを見る
2 RCRiccardo Corti Python 823 ms - 2/5 コードを見る
3 Н"Никита "Programmer" Python 835 ms - 5/5 コードを見る
4 KKKaneki Ken Python 869 ms - 3/5 コードを見る
5 ฐศฐิติพร ศิลป์ประกอบ Python 878 ms - 3/5 コードを見る
6 LMLaura Mahlow Python 884 ms - 4/5 コードを見る
7 HHinne C++ 3,191 ms - 3/5 コードを見る
8 NNNhân Nguyễn Thiện C++ 3,293 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が8件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。