121. Capitals Used Right

A style checker validates how capital letters are used inside a single word made of English letters. The use of capitals is acceptable in exactly three cases: every letter is a capital, no letter is a capital, or only the very first letter is a capital and all the others are lower case.

Return true if the word follows one of these three patterns and false otherwise. A word of one letter is always acceptable. Count the capitals once and compare the count with the length and with the position of the first letter.

Example 1

Input:
word = "PYTHON"
Output:
true
Explanation:

All letters are capitals, which is allowed.

Example 2

Input:
word = "rUst"
Output:
false
Explanation:

A capital in the second position breaks all three patterns, so the answer is false.

Constraints

1 ≤ word.length ≤ 105

word contains only English letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…