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:
| 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:
class Solution:
def detectCapitalUse(self, word: str) -> bool:
...Examples
Example 1:
word = "USA"Every character is uppercase.
So the answer is:
TrueExample 2:
word = "leetcode"Every character is lowercase.
So the answer is:
TrueExample 3:
word = "Google"Only the first letter is uppercase.
All remaining letters are lowercase.
So the answer is:
TrueExample 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:
FalseFirst 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 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:
word = "FlaG"The uppercase letters are:
F, GThe uppercase count is 2.
The word length is 4.
So it is neither all uppercase, nor all lowercase, nor first-letter-only uppercase.
Algorithm
- Count uppercase letters in
word. - If the count is
0, returnTrue. - If the count equals
len(word), returnTrue. - If the count is
1and the first character is uppercase, returnTrue. - 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
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 = 0Scan every character:
for ch in word:If the character is uppercase, increment the count:
if ch.isupper():
uppercase_count += 1Then check the three valid cases:
uppercase_count == 0means 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:
| 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 |