Difficulty: Easy | Acceptance: 56.60% | Paid: No Topics: String
We define the usage of capitals in a word to be right when one of the following cases holds:
This word uses all 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 is right.
- Examples
- Constraints
- Approach 1: Built-in String Methods
- Approach 2: Iterative Counting
- Approach 3: Regular Expression
Examples
Example 1
Input:
word = "USA"
Output:
true
Example 2
Input:
word = "FlaG"
Output:
false
Constraints
1 <= word.length <= 100
word consists of lowercase and uppercase English letters.
Approach 1: Built-in String Methods
Intuition Most programming languages provide standard library methods to check the case of strings or characters. We can simply check if the word matches one of the three valid patterns: all uppercase, all lowercase, or title case (first letter uppercase, rest lowercase).
Steps
- Check if the word is entirely uppercase.
- Check if the word is entirely lowercase.
- Check if the word follows title case rules (first char uppercase, rest lowercase).
- Return true if any of the above checks pass.
class Solution:
def detectCapitalUse(self, word: str) -> bool:
return word.isupper() or word.islower() or word.istitle()Complexity
- Time: O(n) — Standard string methods iterate through the string.
- Space: O(1) — No extra space proportional to input size is used.
- Notes: Very readable and concise, though it may create temporary string copies depending on language implementation.
Approach 2: Iterative Counting
Intuition We can iterate through the string and count how many capital letters are present. The word is valid if the count of capitals is 0 (all lowercase), equal to the length of the word (all uppercase), or exactly 1 and the first character is uppercase.
Steps
- Initialize a counter for capital letters.
- Iterate through each character in the string.
- If the character is uppercase, increment the counter.
- After the loop, check the three conditions: count is 0, count equals length, or (count is 1 and the first character is uppercase).
class Solution:
def detectCapitalUse(self, word: str) -> bool:
n = len(word)
cap_count = 0
for c in word:
if c.isupper():
cap_count += 1
return cap_count == 0 or cap_count == n or (cap_count == 1 and word[0].isupper())Complexity
- Time: O(n) — We traverse the string once.
- Space: O(1) — We only use a few integer variables.
- Notes: Efficient and avoids creating new string objects.
Approach 3: Regular Expression
Intuition The problem can be modeled as a pattern matching exercise. We can define a regular expression that matches the three valid patterns: all uppercase, all lowercase, or title case.
Steps
- Construct a regex pattern that matches the string start to end.
- The pattern should match:
[A-Z]*(all caps),[a-z]*(all lower), or[A-Z][a-z]*(title case). - Test the input word against this pattern.
import re
class Solution:
def detectCapitalUse(self, word: str) -> bool:
return bool(re.fullmatch(r'[A-Z]*|[a-z]*|[A-Z][a-z]*', word))Complexity
- Time: O(n) — Regex engines generally scan the string.
- Space: O(1) — Constant space for the regex pattern structure.
- Notes: Concise one-liner, but regex can be slower than simple iteration for very small strings and harder to debug for beginners.