Skip to content

LeetCode 520: Detect Capital

A clear explanation of checking whether a word uses capital letters correctly by counting uppercase letters.

Problem Restatement

We are given a string word.

We need to return true if the word uses capital letters correctly.

A word uses capitals correctly if it matches one of these three patterns:

PatternExample
All letters are uppercase"USA"
All letters are lowercase"leetcode"
Only the first letter is uppercase"Google"

Otherwise, return false.

The official problem asks us to return whether the usage of capitals in word is right. The string contains lowercase and uppercase English letters.

Input and Output

ItemMeaning
InputA string word
OutputTrue or False
Valid case 1All uppercase
Valid case 2All lowercase
Valid case 3First letter uppercase, rest lowercase

Function shape:

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        ...

Examples

Example 1:

word = "USA"

Every character is uppercase.

So the answer is:

True

Example 2:

word = "leetcode"

Every character is lowercase.

So the answer is:

True

Example 3:

word = "Google"

Only the first letter is uppercase.

All remaining letters are lowercase.

So the answer is:

True

Example 4:

word = "FlaG"

The first letter is uppercase, but the last letter is also uppercase.

This does not match any valid pattern.

So the answer is:

False

First Thought: Check the Three Rules Directly

We can test whether the word is:

word.upper()

or:

word.lower()

or:

word.capitalize()

For example:

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        return (
            word == word.upper()
            or word == word.lower()
            or word == word.capitalize()
        )

This is short and valid in Python.

However, for an interview-style solution, counting uppercase letters makes the logic clearer and language-independent.

Key Insight

The word is valid if the number of uppercase letters has one of these forms:

Uppercase countMeaning
0All lowercase
len(word)All uppercase
1 and word[0] is uppercaseOnly first letter is uppercase

Every other uppercase pattern is invalid.

For example:

word = "FlaG"

The uppercase letters are:

F, G

The uppercase count is 2.

The word length is 4.

So it is neither all uppercase, nor all lowercase, nor first-letter-only uppercase.

Algorithm

  1. Count uppercase letters in word.
  2. If the count is 0, return True.
  3. If the count equals len(word), return True.
  4. If the count is 1 and the first character is uppercase, return True.
  5. Otherwise, return False.

Correctness

There are exactly three valid capitalization patterns.

If the uppercase count is 0, then every letter is lowercase, so the word is valid.

If the uppercase count equals the word length, then every letter is uppercase, so the word is valid.

If the uppercase count is 1 and the first letter is uppercase, then the only uppercase letter is the first letter. Therefore the word is valid.

If none of these cases holds, then the word has an uppercase pattern outside the allowed rules. Therefore it is invalid.

Thus the algorithm returns True exactly for valid capital usage.

Complexity

MetricValueWhy
TimeO(n)We scan the word once
SpaceO(1)Only a counter is stored

Here, n = len(word).

Implementation

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        uppercase_count = 0

        for ch in word:
            if ch.isupper():
                uppercase_count += 1

        return (
            uppercase_count == 0
            or uppercase_count == len(word)
            or (
                uppercase_count == 1
                and word[0].isupper()
            )
        )

Code Explanation

Start with zero uppercase letters:

uppercase_count = 0

Scan every character:

for ch in word:

If the character is uppercase, increment the count:

if ch.isupper():
    uppercase_count += 1

Then check the three valid cases:

uppercase_count == 0

means all lowercase.

uppercase_count == len(word)

means all uppercase.

uppercase_count == 1 and word[0].isupper()

means only the first letter is uppercase.

If one of these conditions is true, the word is valid.

Testing

def test_detect_capital_use():
    s = Solution()

    assert s.detectCapitalUse("USA") is True
    assert s.detectCapitalUse("leetcode") is True
    assert s.detectCapitalUse("Google") is True
    assert s.detectCapitalUse("FlaG") is False
    assert s.detectCapitalUse("g") is True
    assert s.detectCapitalUse("G") is True
    assert s.detectCapitalUse("mL") is False

    print("all tests passed")

Test meaning:

TestWhy
"USA"All uppercase
"leetcode"All lowercase
"Google"First letter uppercase only
"FlaG"Invalid mixed uppercase
Single lowercase letterMinimum lowercase case
Single uppercase letterMinimum uppercase case
"mL"Uppercase after lowercase is invalid