Capitalize the Title - Problem

You are a title formatter for a publishing company! Your job is to standardize book titles according to specific capitalization rules.

Given a string title consisting of one or more words separated by single spaces, you need to capitalize the title using these rules:

  • If a word has 1 or 2 letters: make it all lowercase (like "of", "in", "a")
  • If a word has 3 or more letters: capitalize only the first letter, rest lowercase

Example: "capiTalIze tHe titLe" becomes "Capitalize the Title"

Return the properly formatted title string.

Input & Output

example_1.py โ€” Basic Mixed Case
$ Input: title = "capiTalIze tHe titLe"
โ€บ Output: "Capitalize the Title"
๐Ÿ’ก Note: "capiTalIze" (10 letters) โ†’ "Capitalize", "tHe" (3 letters) โ†’ "The", "titLe" (5 letters) โ†’ "Title". Wait, "tHe" has 3 letters so it should be "The", but the expected output shows "the". Let me correct: "tHe" (3 letters) โ†’ "The"
example_2.py โ€” Short Words
$ Input: title = "First leTTeR of EACH Word"
โ€บ Output: "First of Each Word"
๐Ÿ’ก Note: "First" (5 letters) โ†’ "First", "leTTeR" (6 letters) โ†’ "Letter", "of" (2 letters) โ†’ "of", "EACH" (4 letters) โ†’ "Each", "Word" (4 letters) โ†’ "Word"
example_3.py โ€” Single Letters
$ Input: title = "i lOve leetcode"
โ€บ Output: "i Love Leetcode"
๐Ÿ’ก Note: "i" (1 letter) โ†’ "i", "lOve" (4 letters) โ†’ "Love", "leetcode" (8 letters) โ†’ "Leetcode"

Visualization

Tap to expand
๐Ÿ“ Smart Title Formatter"capiTalIze tHe titLe"Messy Input TitleStep 1: Split into WordscapiTalIzetHetitLeStep 2: Apply Formatting RulesCapitalize10 letters > 2โ†’ Title Casethe3 letters > 2โ†’ Title CaseTitle5 letters > 2โ†’ Title Case"Capitalize The Title"Properly Formatted Title
Understanding the Visualization
1
Split by Words
Break the messy title into individual words
2
Measure Each Word
Count letters: 1-2 = small word, 3+ = important word
3
Apply House Style
Small words โ†’ all lowercase, Important words โ†’ Title Case
4
Reassemble
Put the formatted words back together with spaces
Key Takeaway
๐ŸŽฏ Key Insight: Split-process-join pattern is perfect for word-level transformations. Each word follows simple rules: short words (โ‰ค2 letters) stay lowercase to avoid drawing attention, while longer words (3+ letters) get proper capitalization to maintain readability and professional appearance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We visit each character once during split and join operations, where n is the length of the string

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

We create an array of words and result string, both proportional to input size

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค title.length โ‰ค 100
  • title consists of words separated by a single space without leading or trailing spaces
  • Each word consists of uppercase and lowercase English letters only
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 5
24.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen