Skip to main content

Detect Capital - Solution & Explanation

EasyString11 min readAsked at: Microsoft, Meta, Google +1
Practice this problem

Problem Statement

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

  • All letters in this word are capitals, like "USA".
  • All letters in this word are not capitals, like "leetcode".
  • Only the first letter in this word is capital, like "Google".

Given a string word, return true if the usage of capitals in it is right.

 

Example 1:

Input: word = "USA"
Output: true

Example 2:

Input: word = "FlaG"
Output: false

 

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase and uppercase English letters.

Approach Overview

Problem Overview: You receive a word and must verify whether its capitalization follows valid rules. A word is correct if all letters are uppercase ("USA"), all letters are lowercase ("leetcode"), or only the first letter is uppercase ("Google"). Any other combination should return false.

Approach 1: Check All Conditions Manually (O(n) time, O(1) space)

The most direct solution scans the string and validates capitalization patterns. First inspect the first two characters to determine the expected rule: either all letters should be uppercase or the rest should be lowercase. Then iterate through the remaining characters and verify they follow that rule. For example, if the first two letters are uppercase, every subsequent character must also be uppercase; otherwise all characters after the first must be lowercase. This approach uses simple character checks like isupper() and islower() and requires only one pass over the string.

This method works well because the capitalization rule becomes predictable after observing the first two letters. You only perform constant-time character checks while iterating once through the word. The algorithm runs in O(n) time where n is the length of the word, and uses O(1) extra space. Since the problem focuses purely on character inspection, it falls under typical string processing tasks.

Approach 2: Regular Expression Matching (O(n) time, O(1) space)

A concise alternative is to validate the word using a regular expression. The three valid patterns can be expressed as a single regex: all uppercase ([A-Z]+), all lowercase ([a-z]+), or first uppercase followed by lowercase ([A-Z][a-z]+). Combine them with an OR condition and check if the entire word matches the pattern. If the match succeeds, the capitalization is valid.

This solution is compact and expressive, especially in languages with strong regex support like Python, Java, and JavaScript. The regex engine still scans the entire string once, giving O(n) time complexity and O(1) extra space. Regex-based solutions are common in string validation problems and can also appear in tasks involving regular expression pattern matching.

Recommended for interviews: The manual condition check is usually preferred. It demonstrates that you understand the capitalization rules and can translate them into simple logic with a single pass over the string. Regex solutions are shorter but may hide the underlying reasoning. Showing the manual approach first proves problem-solving clarity, while mentioning the regex alternative demonstrates familiarity with practical string validation techniques.

Approach 1: Check All Conditions Manually

This approach manually checks each of the three conditions for correct capitalization. You can iterate through the word to verify that it either matches all caps, all lowercase, or the first letter is the only capital letter.

In this C solution, we iterate through the given word and count the number of capital letters. We then check for three conditions:

  • If all letters are capitals.
  • If no letters are capitals.
  • If only the first letter is capital and the rest are lower.
  • We return true if any of these conditions are satisfied; otherwise, we return false.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the word.
Space Complexity: O(1), as we only use a few variables.

Try this approach in the editor →

Approach 2: Regular Expression Matching

Using regular expressions, we can check for proper capitalization by defining patterns that match accepted formats: all uppercase, all lowercase, or initial capital only.

In this Python solution, we apply a regular expression to match words fully capitalized, fully lowercase, or starting with one capital followed by lowercase letters. The re.match function verifies if the string fits any pattern. Return true if matched, false otherwise.

Code

Python

Java

JavaScript

C#

Complexity

Time Complexity: O(n), considering regex must check each letter.
Space Complexity: O(1), since regex requires negligible extra space.

Try this approach in the editor →

Approach 3: Count the Number of Uppercase Letters

We can count the number of uppercase letters in the string, and then determine whether it meets the requirements of the problem based on the number of uppercase letters.

  • If the number of uppercase letters is 0 or equal to the length of the string, then return true.
  • If the number of uppercase letters is 1 and the first letter is an uppercase letter, then return true.
  • Otherwise, return false.

The time complexity is O(n), where n is the length of the string word. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Check All Conditions Manually

Time Complexity: O(n), where n is the length of the word.
Space Complexity: O(1), as we only use a few variables.

Regular Expression Matching

Time Complexity: O(n), considering regex must check each letter.
Space Complexity: O(1), since regex requires negligible extra space.

Count the Number of Uppercase Letters—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Check All Conditions ManuallyO(n)O(1)Best for interviews and when you want explicit control over capitalization logic
Regular Expression MatchingO(n)O(1)Useful when regex support is available and concise pattern validation is preferred

Video Solution

Detect capital | Leetcode #520 • Techdose • 8,951 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Detect Capital easy or hard?
Detect Capital is classified as an Easy problem on LeetCode. It mainly tests basic string traversal, character classification, and conditional logic rather than complex algorithms or data structures.
Detect Capital Python/Java solution
In Python or Java, you iterate through the characters and use built-in checks like isupper() and islower() to validate the rule. Another option is using a regular expression to match patterns like [A-Z]+, [a-z]+, or [A-Z][a-z]+. Both implementations run in O(n) time.
How to solve Detect Capital in O(n)?
Iterate through the string once while checking capitalization rules. Determine the expected pattern from the first two characters, then verify that every remaining character follows the rule. Each character is checked exactly once, giving O(n) time and constant space usage.
What is the best approach for Detect Capital?
The most common approach checks capitalization rules manually with a single pass through the string. By examining the first two characters, you determine whether the rest should be uppercase or lowercase and validate accordingly. This runs in O(n) time and O(1) space and is typically the approach interviewers expect.
Is Detect Capital asked at Google/Amazon/Meta?
Detect Capital is a classic easy-level string validation problem often used in early interview rounds. Similar capitalization or string validation questions appear in interviews at companies like Amazon, Google, and Meta to test basic string manipulation skills.
What data structure is used in Detect Capital?
The problem mainly uses simple string traversal and character checks. No additional data structures such as arrays, hash maps, or stacks are required, making it a straightforward string-processing problem.
What is the time complexity of Detect Capital?
The standard solution scans the word once and performs constant-time checks on each character. This results in O(n) time complexity where n is the length of the word, and O(1) space since no additional data structures are required.

Ready to solve this problem?

Practice Detect Capital with our built-in code editor and test cases.

Practice on FleetCode