Detect Capital

Easy
#0505Time: O(n), where n is the length of the word. Each string operation (`toUpperCase`, `toLowerCase`, `substring`, `equals`) takes O(n) time. We perform a constant number of these operations.Space: O(n), where n is the length of the word. This is due to the creation of new strings to hold the uppercase, lowercase, and title-case versions of the word for comparison.
Data structures

Prompt

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 <= 100
  • word consists of lowercase and uppercase English letters.

Approaches

3 approaches with complexity analysis and trade-offs.

This approach directly simulates the three valid capitalization rules by creating modified versions of the input string (all uppercase, all lowercase, title case) and comparing them with the original word.

Algorithm

  • Check if the word is equal to its all-uppercase version (word.toUpperCase()).
  • Check if the word is equal to its all-lowercase version (word.toLowerCase()).
  • Construct a "Title Case" version of the word by capitalizing the first letter and making the rest lowercase. Check if the word is equal to this new string.
  • If any of the above three checks pass, the usage is correct, so return true.
  • If all checks fail, return false.

Walkthrough

The core idea is to check if the input word matches any of the three allowed formats. We can generate a string for each valid format based on the input word and then perform a string comparison.

  1. All Caps: Check if word.equals(word.toUpperCase()).
  2. All Lowercase: Check if word.equals(word.toLowerCase()).
  3. Title Case: Check if word is equal to its title-cased version. This is constructed by taking the first character, making it uppercase, and appending the rest of the string in lowercase.

If any of these conditions are true, the word's capitalization is correct.

class Solution {    public boolean detectCapitalUse(String word) {        if (word.length() <= 1) {            return true;        }        // Case 1: All letters are capitals        boolean allCaps = word.equals(word.toUpperCase());        // Case 2: All letters are not capitals        boolean allLower = word.equals(word.toLowerCase());        // Case 3: Only the first letter is capital        String titleCase = word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();        boolean isTitleCase = word.equals(titleCase);         return allCaps || allLower || isTitleCase;    }}

Complexity

Time

O(n), where n is the length of the word. Each string operation (`toUpperCase`, `toLowerCase`, `substring`, `equals`) takes O(n) time. We perform a constant number of these operations.

Space

O(n), where n is the length of the word. This is due to the creation of new strings to hold the uppercase, lowercase, and title-case versions of the word for comparison.

Trade-offs

Pros

  • Very simple and easy to understand.

  • Leverages built-in string functions, leading to concise code.

Cons

  • Inefficient in terms of memory usage as it creates several new temporary strings.

  • Can be slower than manual iteration due to the overhead of creating and comparing entire strings.

Solutions

class Solution {public:  bool detectCapitalUse(string word) {    int cnt = 0;    for (char c : word)      if (isupper(c))        ++cnt;    return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));  }};

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.