Detect Capital

Capitals are used correctly in a word if one of these holds: every letter is a capital (`"USA"`), no letter is a capital (`"leetcode"`), or only the first letter is a capital (`"Google"`). Given a string `word`, return `true` if its capitals are used correctly.

30:00

Detect Capital
easy
Free written solution
Topics
Companies

Capitals are used correctly in a word if one of these holds: every letter is a capital ("USA"), no letter is a capital ("leetcode"), or only the first letter is a capital ("Google"). Given a string word, return true if its capitals are used correctly.

Example 1:
Input: {"word":"USA"}
Output: true
Constraints:
  • 1≤word.length≤1001 \leq \text{word.length} \leq 100

  • word consists of lowercase and uppercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"word":"USA"}

word; capitals are raised and blue once read

U

0

S

1

A

2

capitals 0

of

length 3

ALL CAPITALS

USA

✓ still possible

all lowercase

leetcode

✓ still possible

Only first capital

Google

✓ still possible

Count the capital letters. The three allowed shapes are: all capitals, no capitals, or exactly one capital in the first position.

Variables
VariableValue
capitals0
DepthFunction Call
Stack empty
0/8