Domain 1EasyCORE

Detect Capital

The problem requires us to determine if the capitalization in a given word is correct based on specific rules. The input is a non-empty string consisting of uppercase and lowercase Latin letters. The output should be a boolean indicating whether the word's capitalization follows one of the three valid patterns.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

520. Detect Capital

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 54.1%


Problem Breakdown

The problem requires us to determine if the capitalization in a given word is correct based on specific rules. The input is a non-empty string consisting of uppercase and lowercase Latin letters. The output should be a boolean indicating whether the word’s capitalization follows one of the three valid patterns.

Inputs:

  • A single string word containing only uppercase and lowercase Latin letters.

Outputs:

  • A boolean value (True or False) indicating if the word’s capitalization follows one of the three valid patterns:
    1. All letters are uppercase (e.g., “USA”).
    2. All letters are lowercase (e.g., “leetcode”).
    3. Only the first letter is uppercase, and the rest are lowercase (e.g., “Google”).

Implicit Constraints:

  • The input string will always be non-empty.
  • The input string will only contain Latin letters.

Tricky Edge Cases:

  • A single-character word (e.g., “A”, “a”) should return True since it trivially satisfies all three conditions.
  • Words with mixed cases but not following the specified patterns (e.g., “FlaG”) should return False.

Intuition

The brute-force approach would involve checking each character in the string to determine if it follows one of the three valid capitalization patterns. This approach is straightforward but inefficient, especially for longer strings, as it requires multiple passes through the string.

A better approach involves leveraging Python’s built-in string methods to check the conditions more efficiently. Specifically, we can use str.isupper(), str.islower(), and str.istitle() methods to determine if the entire word is uppercase, lowercase, or title-cased (first letter uppercase, rest lowercase), respectively.

Approach

  1. Check if all letters are uppercase: Use word.isupper().
  2. Check if all letters are lowercase: Use word.islower().
  3. Check if only the first letter is uppercase and the rest are lowercase: Use word.istitle().

If any of these conditions are met, return True. Otherwise, return False.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string. This is because each of the string methods (isupper(), islower(), istitle()) scans the entire string once.
  • Space Complexity: O(1), as we are not using any additional data structures that grow with the input size.

Solution

def detectCapitalUse(word: str) -> bool:
    return word.isupper() or word.islower() or word.istitle()

Edge Cases Handled

  • Single-character words: The methods isupper(), islower(), and istitle() all handle single-character strings correctly, so the solution works as expected.
  • Words with mixed cases but not following the specified patterns: The function returns False for such cases, as none of the three conditions are met.