Capitalize the Title - Problem

You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:

  • If the length of the word is 1 or 2 letters, change all letters to lowercase.
  • Otherwise, change the first letter to uppercase and the remaining letters to lowercase.

Return the capitalized title.

Input & Output

Example 1 — Mixed Case Input
$ Input: title = "capiTalIze tHe titLe"
Output: "Capitalize The Title"
💡 Note: "capiTalIze" has 10 letters (>2) → "Capitalize", "tHe" has 3 letters (>2) → "The", "titLe" has 5 letters (>2) → "Title"
Example 2 — Short Words
$ Input: title = "First leTTeR of EACH Word"
Output: "First of Each Word"
💡 Note: "First" → "First" (5 letters), "leTTeR" → "Letter" (6 letters), "of" → "of" (2 letters, stays lowercase), "EACH" → "Each" (4 letters), "Word" → "Word" (4 letters)
Example 3 — Very Short Words
$ Input: title = "i lOve Y"
Output: "i love y"
💡 Note: All words have ≤2 letters: "i" (1 letter) → "i", "lOve" (4 letters) → "Love", "Y" (1 letter) → "y"

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

Visualization

Tap to expand
Capitalize the Title: Length-Based RulesInput: "capiTalIze tHe titLe"capiTalIzetHetitLe10 letters > 23 letters > 25 letters > 2Rule: First upper, rest lowerRule: First upper, rest lowerRule: First upper, rest lowerCapitalizeTheTitle✓ Title case✓ Title case✓ Title caseOutput: "Capitalize The Title"
Understanding the Visualization
1
Input
String with mixed capitalization
2
Process
Apply length-based capitalization rules
3
Output
Properly capitalized title
Key Takeaway
🎯 Key Insight: Word length determines the capitalization rule - short words (≤2) stay lowercase, longer words get title case
Asked in
Apple 15 Microsoft 12
25.0K Views
Medium Frequency
~5 min Avg. Time
850 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