Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

520. Detect Capital - Leetcode Solution

Problem Description

The Detect Capital problem asks you to determine whether the usage of capital letters in a given word is correct. According to the problem, a word is said to use capitals correctly if one of the following conditions is true:

  • All letters in the word are uppercase (e.g., "USA").
  • All letters in the word are lowercase (e.g., "leetcode").
  • Only the first letter in the word is uppercase, and the rest are lowercase (e.g., "Google").

You are given a single string word and need to return true if it uses capital letters correctly, and false otherwise.

Thought Process

The problem is about checking if a word follows one of three rules regarding capitalization. The brute-force way would be to check each character and compare its case. However, we can use built-in string methods to check each condition efficiently.

  • First, check if all characters are uppercase using a built-in method.
  • Second, check if all characters are lowercase.
  • Third, check if only the first character is uppercase and the rest are lowercase.

We don't need to check every possible combination of uppercase and lowercase letters. Instead, we focus on these three valid patterns, which simplifies the logic and avoids unnecessary complexity.

Solution Approach

Let's break down the algorithm step by step:

  1. Check if all letters are uppercase:
    • Use the isupper() method (or equivalent) to check if the entire string is in uppercase.
  2. Check if all letters are lowercase:
    • Use the islower() method to check if all characters are lowercase.
  3. Check if only the first letter is uppercase and the rest are lowercase:
    • Check if the first character is uppercase and the substring from the second character onwards is all lowercase.
  4. Return true if any of these checks pass; otherwise, return false.

This approach is efficient because it leverages string methods optimized in the standard library and avoids manual iteration or unnecessary checks.

Example Walkthrough

Let's walk through an example with the input word = "FlaG":

  1. Check if all letters are uppercase: "FlaG".isupper() returns false.
  2. Check if all letters are lowercase: "FlaG".islower() returns false.
  3. Check if only the first letter is uppercase and the rest are lowercase:
    • "FlaG"[0].isupper() is True (since 'F' is uppercase).
    • "FlaG"[1:].islower() is False (since 'l' is lowercase, but 'a' is lowercase, and 'G' is uppercase).
  4. All checks fail, so the function returns false.

For word = "Google":

  1. Check if all letters are uppercase: false.
  2. Check if all letters are lowercase: false.
  3. First is uppercase, rest are lowercase: "Google"[0].isupper() is True, "Google"[1:].islower() is True.
  4. So, the function returns true.

Code Implementation

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        return (
            word.isupper() or
            word.islower() or
            (word[0].isupper() and word[1:].islower())
        )
      
class Solution {
public:
    bool detectCapitalUse(string word) {
        if (all_of(word.begin(), word.end(), ::isupper)) return true;
        if (all_of(word.begin(), word.end(), ::islower)) return true;
        if (isupper(word[0]) && all_of(word.begin() + 1, word.end(), ::islower)) return true;
        return false;
    }
};
      
class Solution {
    public boolean detectCapitalUse(String word) {
        if (word.equals(word.toUpperCase())) return true;
        if (word.equals(word.toLowerCase())) return true;
        if (Character.isUpperCase(word.charAt(0)) && word.substring(1).equals(word.substring(1).toLowerCase())) return true;
        return false;
    }
}
      
var detectCapitalUse = function(word) {
    return (
        word === word.toUpperCase() ||
        word === word.toLowerCase() ||
        (word[0] === word[0].toUpperCase() && word.slice(1) === word.slice(1).toLowerCase())
    );
};
      

Time and Space Complexity

Brute-force approach:

  • You could check each character manually for all three rules, iterating through the string multiple times. This would still be O(n), where n is the length of the string, but with more code and less clarity.
Optimized approach:
  • Each string method (isupper(), islower()) or equivalent scans the string once, so the time complexity is O(n), where n is the length of the input word.
  • Space complexity is O(1) since we are not using any extra data structures proportional to the input size.

Thus, the solution is efficient and optimal for this problem.

Summary

The Detect Capital problem boils down to checking if a word fits one of three capitalization patterns. By leveraging built-in string methods, we can check each rule simply and efficiently, avoiding unnecessary manual iteration. The solution is concise, readable, and runs in linear time with constant space. This approach highlights the power of understanding both the problem constraints and the language's standard library.