Detect Capital — Solution in Python, Java & C++
Detect Capital solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.
Detect Capital is an easy coding problem on strings, asked in Amazon, Google and Cognizant interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Google, Cognizant
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
We define the usage of capitals in a word to be right when one of these holds:
- All letters are capitals, like
"USA". - No letter is a capital, like
"leetcode". - Only the first letter is a capital, like
"Google".
Given a string word, return true if the capitals are used correctly.
Example 1
Input: word = "USA"
Output: true
Example 2
Input: word = "FlaG"
Output: false
Example 3
Input: word = "leetcode"
Output: true
Constraints
1 <= word.length <= 40word consists of upper and lowercase English letters.
Hints for Detect Capital
Hint 1
Count the capitals in one pass — the three legal patterns are distinguishable from that count alone.
Hint 2
All capitals means the count equals the length; no capitals means the count is zero.
Hint 3
Exactly one capital is legal only when it is at index 0.
How to solve Detect Capital
The number of capitals decides it: all, none, or exactly one that sits at index 0.
Approach
- Count the uppercase letters.
- Return
trueif the count is0or equals the length. - Otherwise return
trueonly if the count is1andword[0]is uppercase.
Why it works
The three legal patterns correspond to capital counts of n, 0, and 1-at-the-front; any other count or position is illegal.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- A single-letter word is legal in either case and falls out of the first check automatically.
Reference solution
Python
def detectCapitalUse(word: str) -> bool:
upper = sum(1 for ch in word if ch.isupper())
if upper == len(word) or upper == 0:
return True
return upper == 1 and word[0].isupper()Java
public static boolean detectCapitalUse(String word) {
int upper = 0;
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) upper++;
}
if (upper == word.length() || upper == 0) return true;
return upper == 1 && Character.isUpperCase(word.charAt(0));
}C++
bool detectCapitalUse(string word) {
int upper = 0;
for (char ch : word) {
if (ch >= 'A' && ch <= 'Z') upper++;
}
if (upper == (int) word.size() || upper == 0) return true;
return upper == 1 && word[0] >= 'A' && word[0] <= 'Z';
}JavaScript
var detectCapitalUse = function(word) {
let upper = 0;
for (let i = 0; i < word.length; i++) {
const c = word[i];
if (c >= "A" && c <= "Z") upper++;
}
if (upper === word.length || upper === 0) return true;
return upper === 1 && word[0] >= "A" && word[0] <= "Z";
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings