Watch 10 video solutions for Detect Capital, a easy level problem involving String. This walkthrough by Techdose has 8,895 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |