KatInterview

520. Detect Capital

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to validate the capitalization of a given word based on a specific set of rules. A word's capitalization is considered correct if it adheres to one of the following three patterns:

Solution

Detect Capital in Python

class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        n = len(word)
        capital_count = 0

        # Count the number of uppercase letters in the word.
        for char in word:
            if char.isupper():
                capital_count += 1
        
        # A word's capitalization is correct if:
        # 1. All letters are uppercase (capital_count == n).
        # 2. All letters are lowercase (capital_count == 0).
        # 3. Only the first letter is uppercase (capital_count == 1 and the first char is uppercase).
        return capital_count == n or capital_count == 0 or (capital_count == 1 and word[0].isupper())

Detect Capital in Java

class Solution {
    public boolean detectCapitalUse(String word) {
        int n = word.length();
        int capitalCount = 0;

        // Count the number of uppercase letters in the word.
        for (char c : word.toCharArray()) {
            if (Character.isUpperCase(c)) {
                capitalCount++;
            }
        }

        // A word's capitalization is correct if:
        // 1. All letters are uppercase (capitalCount == n).
        // 2. All letters are lowercase (capitalCount == 0).
        // 3. Only the first letter is uppercase (capitalCount == 1 and the first char is uppercase).
        return capitalCount == n || capitalCount == 0 || (capitalCount == 1 && Character.isUpperCase(word.charAt(0)));
    }
}

Detect Capital in C++

class Solution {
public:
    bool detectCapitalUse(string word) {
        int n = word.length();
        int capitalCount = 0;

        // Count the number of uppercase letters in the word.
        for (char c : word) {
            if (isupper(c)) {
                capitalCount++;
            }
        }

        // A word's capitalization is correct if:
        // 1. All letters are uppercase (capitalCount == n).
        // 2. All letters are lowercase (capitalCount == 0).
        // 3. Only the first letter is uppercase (capitalCount == 1 and the first char is uppercase).
        return capitalCount == n || capitalCount == 0 || (capitalCount == 1 && isupper(word[0]));
    }
};

Detect Capital in TypeScript

function detectCapitalUse(word: string): boolean {
    const n = word.length;
    let capitalCount = 0;

    // Count the number of uppercase letters in the word.
    for (let i = 0; i < n; i++) {
        const char = word[i];
        // In JS/TS, we can check if a character is uppercase by its character code.
        // The problem statement guarantees the string consists of English letters.
        if (char >= 'A' && char <= 'Z') {
            capitalCount++;
        }
    }

    // A word's capitalization is correct if:
    // 1. All letters are uppercase (capitalCount == n).
    // 2. All letters are lowercase (capitalCount == 0).
    // 3. Only the first letter is uppercase (capitalCount == 1 and the first char is uppercase).
    return capitalCount === n || capitalCount === 0 || (capitalCount === 1 && word[0] >= 'A' && word[0] <= 'Z');
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.