Detect Capital

Easy

Topics
String

Given a word s, return true if its capital usage is correct: all uppercase, all lowercase, or only the first letter uppercase.

Example 1

USA
capital
Three patterns are allowed: no capitals, all capitals, or exactly one at the front. Counting the capitals and noting whether the first character is one decides all three at once — answer true.
Cells are the characters of the string; the small numbers underneath are indices.
Input:  s = "USA"
Output: true

Example 2

FlaG
capital
Input:  s = "FlaG"
Output: false

Example 3

Google
capital
Input:  s = "Google"
Output: true

Constraints

  • 1 <= s.length <= 100
  • s consists of upper and lowercase English letters.
Editorial — intuition, approach, complexity and pitfalls

Intuition

Three usages are permitted: all caps ("USA"), all lowercase ("leetcode"), or exactly one leading capital ("Google"). Anything else is wrong.

The temptation is to write one clever scan with a running state machine. It is easier and clearer to test the three permitted shapes directly and OR them, because the specification is a disjunction of three cases — mirroring it in code means the reader can check the code against the problem line by line.

A counting approach also works: count the capitals, then accept if the count is 0, or equals the length, or is exactly 1 and sits at index 0. Same cost, and it generalises slightly better if the rules change.

Approach

  1. s.isupper() — all letters capital.
  2. s.islower() — all letters lowercase.
  3. s[0].isupper() and s[1:].islower() — one leading capital, rest lowercase.

Return the OR of the three.

Dry Run

"USA"     isupper -> true, short-circuits                        true
"FlaG"    isupper false, islower false,
          s[0]='F' upper but "laG".islower() is false            false
"Google"  isupper false, islower false,
          'G' upper and "oogle".islower() true                   true
"a"       isupper false, islower true                            true
"A"       isupper true                                           true

The last two are the ones to check deliberately. For a single character the third clause would evaluate s[1:] as the empty string, and "".islower() is false in Python — so "A" only passes because isupper() catches it first. The order of the clauses is doing real work.

Complexity

  • Time — O(n): each predicate scans the string once, so up to three passes — still linear. A single counting pass would do it in one, which matters not at all for the 100-character limit here but is the better answer if asked to minimise passes.
  • Space — O(1) for the predicates themselves, but note s[1:] allocates a copy in Python, making this O(n) space as written. The counting version is genuinely O(1). Worth saying out loud rather than claiming O(1) and being caught.

Common Pitfalls

  • Reordering the clauses so the third is tested first. A single uppercase character then fails, because "".islower() is false.
  • Claiming O(1) space while slicing. s[1:] copies the tail; checking all(c.islower() for c in s[1:]) or counting avoids it.
  • Accepting a capital anywhere so long as there is only one. "gOogle" has exactly one capital and is invalid — position matters.
  • Forgetting that the constraints guarantee at least one character, so s[0] is safe here. If the empty string were possible, the third clause would need a length guard.
  • Using istitle() as a shortcut. It is close but not the same: it treats each word separately and would accept multi-word input that this problem does not have, so it passes here by luck rather than by meaning.

Full Solution — all languages

class Solution:
    def detectCapitalUse(self, s):
        return s.isupper() or s.islower() or (s[0].isupper() and s[1:].islower())
SensAI
Beta
Tap the mic to stop voice mode