Date: 2026-06-06
Time: 15:32
capitalize-the-title/solution.pyThis file is the complete solution and test suite for LeetCode 2129: Capitalize the Title. It owns both the algorithm implementation and its verification. Like every other problem directory in this repo, it follows a self-contained pattern: one Solution class with the LeetCode-expected method signature, plus inline unittest tests.
Solution.capitalizeTitle(self, title: str) -> str — The core algorithm. Takes a space-separated title string and applies two rules per word:
Returns the transformed title as a single string with words joined by spaces.
TestCapitalizeTitle — Eight test cases covering:
"aB"), single long word ("hELLO"), single character ("Z"), all-short words, and the boundary case of exactly 3 characters ("THE")The solution uses a generator expression inside str.join — the idiomatic Python approach for word-by-word string transformations. Rather than building a list and joining, it streams transformed words directly.
The title-casing is done manually (w[0].upper() + w[1:].lower()) instead of using Python's built-in str.title(). This is intentional: str.title() doesn't have the length-based conditional, so you'd still need the branch, and .capitalize() would work but this explicit form makes the transformation visible.
The file bundles solution and tests in a single module with if _name == "main_": unittest.main(), matching the repo-wide convention. Tests use setUp to instantiate Solution once per test method.
Imports: Only unittest from the standard library — no external dependencies.
Imported by: The capitalize-the-title/testsolution.py file imports from this module. The massive "Imported By" list in the prompt is misleading — those are test files across the entire repo that import unittest, not this specific solution. The actual dependent is just testsolution.py in the same directory.
1. title.split() tokenizes on whitespace, producing a list of words
2. For each word, check len(w) <= 2
3. If short: w.lower() — full lowercase
4. If long: w[0].upper() + w[1:].lower() — uppercase first char, lowercase the rest
5. " ".join(...) reassembles with single spaces
The entire transformation is a single return statement — no intermediate state, no mutation.
split() without arguments is safe — no punctuation or multi-space handling needed.<= 2, meaning exactly-3-letter words get title-cased. The test testthreeletter_word verifies this boundary.w[1:] on a word of length >= 3 is always non-empty, so no index errors.None. The LeetCode contract guarantees valid input (non-empty title, only English letters and spaces, no leading/trailing spaces, no consecutive spaces). The solution trusts those preconditions and does no validation — appropriate for a competitive programming context.