LeetCode Problem Workspace
Detect Capital
Detect Capital asks you to determine if a word follows a correct capital usage pattern based on specific rules.
1
Topics
5
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Detect Capital asks you to determine if a word follows a correct capital usage pattern based on specific rules.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
In the Detect Capital problem, the task is to determine if a word's capital usage follows a set of rules. These rules allow for either all letters to be uppercase, all lowercase, or only the first letter to be uppercase. A string-driven solution helps us efficiently evaluate these conditions.
Problem Statement
You are given a string word, which contains only uppercase and lowercase English letters. Your goal is to determine if the capital usage in the word is correct based on the following rules:
- All letters in the word are uppercase, 2. All letters in the word are lowercase, 3. Only the first letter is uppercase, and the rest are lowercase. Return
trueif any of these rules are satisfied, otherwise returnfalse.
Examples
Example 1
Input: word = "USA"
Output: true
Example details omitted.
Example 2
Input: word = "FlaG"
Output: false
Example details omitted.
Constraints
- 1 <= word.length <= 100
- word consists of lowercase and uppercase English letters.
Solution Approach
Check for all uppercase
First, check if the entire word is uppercase using a simple string comparison with .upper(). If it matches, the word satisfies the first rule, returning true.
Check for all lowercase
Next, check if the word is entirely lowercase using .lower(). If true, return true since this satisfies the second rule.
Check for capital first letter only
Finally, check if the first letter is uppercase and all other letters are lowercase. This can be verified using the string method .istitle(), which checks if the string is in title case (only the first letter capitalized).
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
The time complexity depends on the string length, as we are performing constant-time checks like .upper(), .lower(), and .istitle(). Hence, the time complexity is O(n), where n is the length of the string. Space complexity is O(1) because no additional space is used beyond simple variables for string checks.
What Interviewers Usually Probe
- Candidate efficiently identifies string-driven checks to validate each capitalization rule.
- Candidate might miss the importance of using Python's string methods like
.upper()and.istitle(). - Candidate could misinterpret the rules, for example, returning true for mixed capitalization where it's invalid.
Common Pitfalls or Variants
Common pitfalls
- Confusing mixed capital cases (like 'FlaG') as valid when it does not meet any of the rules.
- Overcomplicating the solution by adding unnecessary conditions or checks.
- Forgetting to handle edge cases such as very short strings like 'A' or 'a'.
Follow-up variants
- Check for variations in capitalization beyond the basic three rules (e.g., alternating case).
- Handle words with punctuation or non-alphabetical characters (though these are out of scope for this problem).
- Extend the solution to work with multiple strings or inputs in batch processing.
FAQ
What are the rules for correct capital usage in the Detect Capital problem?
The correct capital usage can be: all letters are uppercase, all are lowercase, or only the first letter is uppercase and the rest are lowercase.
How can I check if a word is in title case for Detect Capital?
You can use Python's .istitle() method to check if only the first letter of the word is uppercase and all subsequent letters are lowercase.
Is there a time complexity advantage to checking all lowercase or all uppercase first?
Yes, checking if a word is fully lowercase or uppercase can short-circuit the process and quickly determine validity without needing further checks.
What should I do if I get mixed case words like 'FlaG' in the Detect Capital problem?
Mixed case words like 'FlaG' do not satisfy any of the three valid capitalization rules and should return false.
What is the space complexity of solving Detect Capital?
The space complexity is O(1), as no extra space is used beyond simple variable checks for capitalization.
Solution
Solution 1: Count the Number of Uppercase Letters
We can count the number of uppercase letters in the string, and then determine whether it meets the requirements of the problem based on the number of uppercase letters.
class Solution:
def detectCapitalUse(self, word: str) -> bool:
cnt = sum(c.isupper() for c in word)
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward