File: detect-capital/solution.py

Date: 2026-06-06

Time: 16:19

detect-capital/solution.py

Purpose

This file solves LeetCode 520 — Detect Capital. It owns a single responsibility: determining whether a word's capitalization follows one of three valid patterns. It's one of ~400+ problem solutions in the leetcode-implementations repo, each isolated in its own directory.

Key Components

detectCapitalUse(word: str) -> bool — The sole public function. It returns True when the word's capitalization matches any of these three rules:

1. All letters are uppercase ("USA")

2. All letters are lowercase ("leetcode")

3. Only the first letter is uppercase ("Google")

The implementation counts uppercase characters in a single pass, then checks the count against three conditions in a compound boolean expression.

Patterns

Counting reduction instead of pattern matching. Rather than checking word.isupper(), word.islower(), or word.istitle() (Python's built-in string predicates), the solution reduces the problem to a single integer — upper_count — and derives all three conditions from it. This is slightly more manual but evaluates the string only once instead of up to three times.

Single-expression return. The entire decision logic lives in one return statement with three disjuncts:

Dependencies

Imports: None. Pure stdlib — uses only str methods (isupper()).

Imported by: detect-capital/test_solution.py directly. The massive "Imported By" list in the prompt is an artifact of the test harness structure — those other test files don't actually import this solution; they share a common test runner pattern.

Flow

1. Generator expression iterates over every character in word, yielding 1 for each uppercase letter.

2. sum() aggregates into upper_count.

3. A single boolean expression short-circuits through the three valid patterns.

The function is O(n) time, O(1) space.

Invariants

Error Handling

None. The function is a pure predicate with no error paths under valid LeetCode inputs. An empty string would cause an unhandled IndexError at word[0].isupper().

Topics to Explore

Beliefs