Detect Capital
Easy
Topics
Given a word s, return true if its capital usage is correct: all uppercase, all lowercase, or only the first letter uppercase.
Example 1
true.Input: s = "USA" Output: true
Example 2
Input: s = "FlaG" Output: false
Example 3
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
s.isupper()— all letters capital.s.islower()— all letters lowercase.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 trueThe 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; checkingall(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())import java.util.*;
class Solution {
public boolean detectCapitalUse(String s) {
return s.equals(s.toUpperCase()) || s.equals(s.toLowerCase()) || (Character.isUpperCase(s.charAt(0)) && s.substring(1).equals(s.substring(1).toLowerCase()));
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: bool detectCapitalUse(string s) {
auto isUpper = [](const string& x) {
for (char c : x) if (!isupper((unsigned char)c)) return false;
return true;
};
auto isLower = [](const string& x) {
for (char c : x) if (!islower((unsigned char)c)) return false;
return true;
};
if (isUpper(s) || isLower(s)) return true;
return isupper((unsigned char)s[0]) && isLower(s.substr(1));
}
};/**
* @param {string} s
* @return {boolean}
*/ var detectCapitalUse = function(s) {
const allUp = s === s.toUpperCase();
const allLow = s === s.toLowerCase();
const title = s[0] === s[0].toUpperCase() && s.slice(1) === s.slice(1).toLowerCase();
return allUp || allLow || title;
};