Đề bài
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.
Ràng buộc
0 ≤ length of text ≤ 10,000 (ASCII characters)
Ví dụ
Đầu vào:
text = "Hello World"
Kết quả mong đợi:
expected = 2
Đầu vào:
std::string text = "Hello World";
Kết quả mong đợi:
long long expected = 2;
Đầu vào:
string text = "Hello World";
Kết quả mong đợi:
long expected = 2;
Giải thích
H and W are uppercase: 2.
Đầu vào:
text = "no caps here!"
Kết quả mong đợi:
expected = 0
Đầu vào:
std::string text = "no caps here!";
Kết quả mong đợi:
long long expected = 0;
Đầu vào:
string text = "no caps here!";
Kết quả mong đợi:
long expected = 0;
Giải thích
There are no uppercase letters: 0.
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 notice only letters A through Z count.
Gợi ý 2
Check each character’s case directly instead of comparing against words or splitting the string.
Gợi ý 3
Remember lowercase letters, digits, spaces, and punctuation do not contribute.
Độ 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.