We define the usage of capitals in a word to be right when one of the following cases holds:
"USA"."leetcode"."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 <= 100word consists of lowercase and uppercase English letters.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.
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:
We return true if any of these conditions are satisfied; otherwise, we return false.
Time Complexity: O(n), where n is the length of the word.
Space Complexity: O(1), as we only use a few variables.
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.
Python
Java
JavaScript
C#
Time Complexity: O(n), considering regex must check each letter.
Space Complexity: O(1), since regex requires negligible extra space.
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.
true.true.false.The time complexity is O(n), where n is the length of the string word. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| 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 | — |
| 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 |
Detect capital | Leetcode #520 • Techdose • 8,895 views views
Watch 9 more video solutions →Practice Detect Capital with our built-in code editor and test cases.
Practice on FleetCode