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
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
โ Linear Growth
Space Complexity
O(n)
We create an array of words and result string, both proportional to input size
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code