Official

A - Capitalized? Editorial by evima


For beginners: This problem might be somewhat challenging as your very first programming task. If you find it difficult, it is recommended to solve Problem A from another round first (for example: ABC336A). In any case, please refer to Problem A of the practice contest for how to handle Standard Input and Output.


Let’s restate the problem.

  • Determine whether the following condition is satisfied: The first character of the string \(S\) is an uppercase English letter, and all other characters are lowercase.

Approach 1. For loop

Implement the following process. We will call the first character of \(S\) the \(0\)-th character.

  1. Declare a variable \(\mathrm{ans}\) and assign it true if the \(0\)-th character of \(S\) is uppercase, or false if it is lowercase.
  2. For each \(i = 1, 2, \dots, |S| - 1\), do the following:
    • If the \(i\)-th character of \(S\) is uppercase, assign false to \(\mathrm{ans}\).
  3. Print Yes if the value of \(\mathrm{ans}\) is true, and No otherwise.

To determine whether a character is uppercase or lowercase, you can use the standard library of your programming language. In C++, there are functions like isupper and islower, and other languages should have similarly named functions.

Below is a sample implementation in C++.

#include <cctype>
#include <iostream>
using namespace std;

int main() {
    string S;
    cin >> S;
    bool ans = isupper(S[0]);
    for (int i = 1; i < S.size(); ++i) {
        ans &= bool(islower(S[i]));
    }
    cout << (ans ? "Yes" : "No") << endl;
}

Note for C++: The values returned by isupper and islower are not true or false. When \(c\) is an uppercase letter, islower(\(c\)) returns some integer other than zero, and isupper is similar. If you treat this value as a bool, you may have to convert it explicitly, or unexpected failures may occur (omitting the conversion in line 10 of the above code will cause the bitwise operation by &= to fail).


Approach 2. Library

Depending on the language, you can use a regular expression library. Below is a sample implementation in Python.

import re

S = input()
print("Yes" if re.fullmatch("[A-Z][a-z]*", S) else "No")

Additionally, there might be a library function that determines whether all characters in a string are lowercase. Applying that function to \(S\) minus its first character can replace the for loop part of Approach 1.

S = input()
print("Yes" if S[0].isupper() and (len(S) == 1 or S[1:].islower()) else "No")

Note for Python: isupper and islower return False for an empty string, which is a misplaced kindness in competitive programming. Here, we must separately handle the case where the length of \(S\) is one.

Furthermore, there might be a library function that solves this problem in one go.

S = input()
print("Yes" if S.istitle() else "No")

posted:
last update: