Detect Capital - Solution & Explanation
Problem Statement
We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like
"USA". - All letters in this word are not capitals, like
"leetcode". - Only the first letter in this word is capital, like
"Google".
Given a string word, return true if the usage of capitals in it is right.
Example 1:
Input: word = "USA" Output: true
Example 2:
Input: word = "FlaG" Output: false
Constraints:
1 <= word.length <= 100wordconsists of lowercase and uppercase English letters.
Approach Overview
Problem Overview: You receive a word and must verify whether its capitalization follows valid rules. A word is correct if all letters are uppercase ("USA"), all letters are lowercase ("leetcode"), or only the first letter is uppercase ("Google"). Any other combination should return false.
Approach 1: Check All Conditions Manually (O(n) time, O(1) space)
The most direct solution scans the string and validates capitalization patterns. First inspect the first two characters to determine the expected rule: either all letters should be uppercase or the rest should be lowercase. Then iterate through the remaining characters and verify they follow that rule. For example, if the first two letters are uppercase, every subsequent character must also be uppercase; otherwise all characters after the first must be lowercase. This approach uses simple character checks like isupper() and islower() and requires only one pass over the string.
This method works well because the capitalization rule becomes predictable after observing the first two letters. You only perform constant-time character checks while iterating once through the word. The algorithm runs in O(n) time where n is the length of the word, and uses O(1) extra space. Since the problem focuses purely on character inspection, it falls under typical string processing tasks.
Approach 2: Regular Expression Matching (O(n) time, O(1) space)
A concise alternative is to validate the word using a regular expression. The three valid patterns can be expressed as a single regex: all uppercase ([A-Z]+), all lowercase ([a-z]+), or first uppercase followed by lowercase ([A-Z][a-z]+). Combine them with an OR condition and check if the entire word matches the pattern. If the match succeeds, the capitalization is valid.
This solution is compact and expressive, especially in languages with strong regex support like Python, Java, and JavaScript. The regex engine still scans the entire string once, giving O(n) time complexity and O(1) extra space. Regex-based solutions are common in string validation problems and can also appear in tasks involving regular expression pattern matching.
Recommended for interviews: The manual condition check is usually preferred. It demonstrates that you understand the capitalization rules and can translate them into simple logic with a single pass over the string. Regex solutions are shorter but may hide the underlying reasoning. Showing the manual approach first proves problem-solving clarity, while mentioning the regex alternative demonstrates familiarity with practical string validation techniques.
Approach 1: Check All Conditions Manually
This approach manually checks each of the three conditions for correct capitalization. You can iterate through the word to verify that it either matches all caps, all lowercase, or the first letter is the only capital letter.
In this C solution, we iterate through the given word and count the number of capital letters. We then check for three conditions:
- If all letters are capitals.
- If no letters are capitals.
- If only the first letter is capital and the rest are lower.
We return true if any of these conditions are satisfied; otherwise, we return false.
Complexity
Time Complexity: O(n), where n is the length of the word.
Space Complexity: O(1), as we only use a few variables.
Approach 2: Regular Expression Matching
Using regular expressions, we can check for proper capitalization by defining patterns that match accepted formats: all uppercase, all lowercase, or initial capital only.
In this Python solution, we apply a regular expression to match words fully capitalized, fully lowercase, or starting with one capital followed by lowercase letters. The re.match function verifies if the string fits any pattern. Return true if matched, false otherwise.
Code
Python
Java
JavaScript
C#
Complexity
Time Complexity: O(n), considering regex must check each letter.
Space Complexity: O(1), since regex requires negligible extra space.
Approach 3: 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.
- If the number of uppercase letters is 0 or equal to the length of the string, then return
true. - If the number of uppercase letters is 1 and the first letter is an uppercase letter, then return
true. - Otherwise, return
false.
The time complexity is O(n), where n is the length of the string word. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Check All Conditions Manually | Time Complexity: O(n), where n is the length of the word. |
| Regular Expression Matching | Time Complexity: O(n), considering regex must check each letter. |
| Count the Number of Uppercase Letters | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Check All Conditions Manually | O(n) | O(1) | Best for interviews and when you want explicit control over capitalization logic |
| Regular Expression Matching | O(n) | O(1) | Useful when regex support is available and concise pattern validation is preferred |
Video Solution
Detect capital | Leetcode #520 • Techdose • 8,951 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Detect Capital easy or hard?
Detect Capital Python/Java solution
How to solve Detect Capital in O(n)?
What is the best approach for Detect Capital?
Is Detect Capital asked at Google/Amazon/Meta?
What data structure is used in Detect Capital?
What is the time complexity of Detect Capital?
Ready to solve this problem?
Practice Detect Capital with our built-in code editor and test cases.
Practice on FleetCode