Detect Capital - Problem

We define the usage of capitals in a word to be right when one of the following cases holds:

  • All letters in this word are capitals, like "USA"
  • All letters in this word are not capitals, like "leetcode"
  • Only the first letter in this word is capital, like "Google"

Given a string word, return true if the usage of capitals in it is right.

Input & Output

Example 1 — Proper Case
$ Input: word = "Google"
Output: true
💡 Note: First letter 'G' is uppercase, remaining letters 'o', 'o', 'g', 'l', 'e' are lowercase. This matches the third valid pattern.
Example 2 — All Uppercase
$ Input: word = "USA"
Output: true
💡 Note: All letters 'U', 'S', 'A' are uppercase. This matches the first valid pattern.
Example 3 — Invalid Mixed Case
$ Input: word = "FlaG"
Output: false
💡 Note: Mixed case with 'F' and 'G' uppercase, 'l' and 'a' lowercase. This doesn't match any of the three valid patterns.

Constraints

  • 1 ≤ word.length ≤ 100
  • word consists of lowercase and uppercase English letters

Visualization

Tap to expand
Detect Capital: Three Valid PatternsPattern 1ALL UPPER"USA", "API"Pattern 2all lower"hello", "world"Pattern 3First Upper"Google", "Java"Input: "Google"Invalid Examples"FlaG", "gOOgle", "UsA" → false"Google" matches Pattern 3 → true
Understanding the Visualization
1
Input Word
Check capitalization pattern of input string
2
Pattern Match
Verify against three valid patterns
3
Result
Return true if matches any valid pattern
Key Takeaway
🎯 Key Insight: Only three patterns are valid - count uppercase letters to efficiently determine which pattern matches
Asked in
Google 15 Microsoft 12
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen