Detect Capital

Easy
216.

Detect Capital

Detect Capital is an easy coding problem on strings, asked in Amazon, Google and Cognizant interviews.

We define the usage of capitals in a word to be right when one of these holds:

  1. All letters are capitals, like "USA".
  2. No letter is a capital, like "leetcode".
  3. Only the first letter is a capital, like "Google".

Given a string word, return true if the capitals are used correctly.

Example 1

Input
word = "USA"
Output
true

Example 2

Input
word = "FlaG"
Output
false

Example 3

Input
word = "leetcode"
Output
true

Constraints

  • 1 <= word.length <= 40
  • word consists of upper and lowercase English letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Detect Capital — Solution in Python, Java & C++

Detect Capital solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.

Detect Capital is an easy coding problem on strings, asked in Amazon, Google and Cognizant interviews.

  • Difficulty: Easy
  • Topics: Strings
  • Asked at: Amazon, Google, Cognizant
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

We define the usage of capitals in a word to be right when one of these holds:

  1. All letters are capitals, like "USA".
  2. No letter is a capital, like "leetcode".
  3. Only the first letter is a capital, like "Google".

Given a string word, return true if the capitals are used correctly.

Example 1

Input: word = "USA"
Output: true

Example 2

Input: word = "FlaG"
Output: false

Example 3

Input: word = "leetcode"
Output: true

Constraints

  • 1 <= word.length <= 40
  • word consists of upper and lowercase English letters.

Hints for Detect Capital

  1. Hint 1

    Count the capitals in one pass — the three legal patterns are distinguishable from that count alone.

  2. Hint 2

    All capitals means the count equals the length; no capitals means the count is zero.

  3. Hint 3

    Exactly one capital is legal only when it is at index 0.

How to solve Detect Capital

The number of capitals decides it: all, none, or exactly one that sits at index 0.

Approach

  1. Count the uppercase letters.
  2. Return true if the count is 0 or equals the length.
  3. Otherwise return true only if the count is 1 and word[0] is uppercase.

Why it works

The three legal patterns correspond to capital counts of n, 0, and 1-at-the-front; any other count or position is illegal.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • A single-letter word is legal in either case and falls out of the first check automatically.

Reference solution

Python

def detectCapitalUse(word: str) -> bool:
    upper = sum(1 for ch in word if ch.isupper())
    if upper == len(word) or upper == 0:
        return True
    return upper == 1 and word[0].isupper()

Java

public static boolean detectCapitalUse(String word) {
    int upper = 0;
    for (int i = 0; i < word.length(); i++) {
        if (Character.isUpperCase(word.charAt(i))) upper++;
    }
    if (upper == word.length() || upper == 0) return true;
    return upper == 1 && Character.isUpperCase(word.charAt(0));
}

C++

bool detectCapitalUse(string word) {
    int upper = 0;
    for (char ch : word) {
        if (ch >= 'A' && ch <= 'Z') upper++;
    }
    if (upper == (int) word.size() || upper == 0) return true;
    return upper == 1 && word[0] >= 'A' && word[0] <= 'Z';
}

JavaScript

var detectCapitalUse = function(word) {
    let upper = 0;
    for (let i = 0; i < word.length; i++) {
        const c = word[i];
        if (c >= "A" && c <= "Z") upper++;
    }
    if (upper === word.length || upper === 0) return true;
    return upper === 1 && word[0] >= "A" && word[0] <= "Z";
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings