Learnitweb

Problem 520: Detect Capital

You are given a word, and you must determine whether the usage of capital letters is correct based on the following rules:

A word is correctly capitalized if:

  1. All letters are uppercase
    Example: “USA”
  2. All letters are lowercase
    Example: “leetcode”
  3. Only the first letter is uppercase, and the rest are lowercase
    Example: “Google”

Any word that violates these three patterns is incorrect.

Your task is to return true if the capitalization is valid, false otherwise.


Understanding the Problem

Examples:

Input: “USA” → Output: true
Input: “leetcode” → Output: true
Input: “Google” → Output: true
Input: “FlaG” → Output: false
(The ‘a’ is lowercase but ‘G’ is uppercase → invalid pattern)

We are checking the pattern of letters, nothing more.


Approach (Explained in Simple Language)

We must verify whether the word follows one of these valid patterns:

Pattern 1: Entire word is uppercase

Check if every character is between ‘A’ and ‘Z’

Pattern 2: Entire word is lowercase

Check if every character is between ‘a’ and ‘z’

Pattern 3: Only first character uppercase, rest lowercase

Check first letter uppercase
Check rest letters lowecase

If any of these patterns matches, return true.

This approach is simple, intuitive, and efficient.


Java Code

public class DetectCapital {

    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;
    }

    public static void main(String[] args) {
        DetectCapital solution = new DetectCapital();
        System.out.println(solution.detectCapitalUse("USA"));
    }
}

Alternative Manual Checking (Without using built-in toUpperCase)

This version manually verifies patterns, helpful in interviews.

public class DetectCapitalManual {

    public boolean detectCapitalUse(String word) {

        int uppercaseCount = 0;

        for (char c : word.toCharArray()) {
            if (Character.isUpperCase(c)) {
                uppercaseCount++;
            }
        }

        int n = word.length();

        if (uppercaseCount == n) {
            return true;
        }

        if (uppercaseCount == 0) {
            return true;
        }

        if (uppercaseCount == 1 && Character.isUpperCase(word.charAt(0))) {
            return true;
        }

        return false;
    }
}

Dry Run

Let’s dry run using:

Input: “FlaG”

Step 1: Check uppercase word

“FlaG”.toUpperCase() = “FLAG”
Not equal → proceed.

Step 2: Check lowercase word

“FlaG”.toLowerCase() = “flag”
Not equal → proceed.

Step 3: Check first uppercase + rest lowercase

First letter = ‘F’ → uppercase
Remaining = “laG”
“laG”.toLowerCase() = “lag”
Not equal → condition fails.

Return false.

Correct output = false.


Try another:

Input: “Google”

Check uppercase? No
Check lowercase? No
Check first upper + rest lower?

First = ‘G’ → uppercase
Remaining “oogle” is all lowercase
Condition true → return true


One more:

Input: “USA”

uppercase? “USA”.toUpperCase() = “USA” → true
Return true


Why This Approach Works

There are only three valid patterns, and all of them are easy to detect:

Uppercase all
Lowercase all
First uppercase only

Since the problem is just pattern checking, we do not need complex algorithms.

Time complexity: O(n)
Space complexity: O(1)