# LeetCode 520: Detect Capital

## 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:

| Pattern | Example |
|---|---|
| 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

| Item | Meaning |
|---|---|
| Input | A string `word` |
| Output | `True` or `False` |
| Valid case 1 | All uppercase |
| Valid case 2 | All lowercase |
| Valid case 3 | First letter uppercase, rest lowercase |

Function shape:

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

## Examples

Example 1:

```python
word = "USA"
```

Every character is uppercase.

So the answer is:

```python
True
```

Example 2:

```python
word = "leetcode"
```

Every character is lowercase.

So the answer is:

```python
True
```

Example 3:

```python
word = "Google"
```

Only the first letter is uppercase.

All remaining letters are lowercase.

So the answer is:

```python
True
```

Example 4:

```python
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:

```python
False
```

## First Thought: Check the Three Rules Directly

We can test whether the word is:

```python
word.upper()
```

or:

```python
word.lower()
```

or:

```python
word.capitalize()
```

For example:

```python
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 count | Meaning |
|---:|---|
| `0` | All lowercase |
| `len(word)` | All uppercase |
| `1` and `word[0]` is uppercase | Only first letter is uppercase |

Every other uppercase pattern is invalid.

For example:

```python
word = "FlaG"
```

The uppercase letters are:

```text
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

| Metric | Value | Why |
|---|---|---|
| Time | `O(n)` | We scan the word once |
| Space | `O(1)` | Only a counter is stored |

Here, `n = len(word)`.

## Implementation

```python
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:

```python
uppercase_count = 0
```

Scan every character:

```python
for ch in word:
```

If the character is uppercase, increment the count:

```python
if ch.isupper():
    uppercase_count += 1
```

Then check the three valid cases:

```python
uppercase_count == 0
```

means all lowercase.

```python
uppercase_count == len(word)
```

means all uppercase.

```python
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

```python
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:

| Test | Why |
|---|---|
| `"USA"` | All uppercase |
| `"leetcode"` | All lowercase |
| `"Google"` | First letter uppercase only |
| `"FlaG"` | Invalid mixed uppercase |
| Single lowercase letter | Minimum lowercase case |
| Single uppercase letter | Minimum uppercase case |
| `"mL"` | Uppercase after lowercase is invalid |

