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 Letter 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 - Split and Process INPUT Original String: "capiTalIze tHe titLe" Split into words: "capiTalIze" length: 10 "tHe" length: 3 "titLe" length: 5 Rules: - Length 1-2: all lowercase - Length 3+: First UPPER, rest lowercase title = "capiTalIze tHe titLe" ALGORITHM STEPS 1 Split String Split by space delimiter 2 Check Length For each word, get length 3 Apply Rules Transform based on length 4 Join Words Combine with spaces Processing: "capiTalIze" (10 chars) --> "Capitalize" "tHe" (3 chars) --> "The" "titLe" (5 chars) --> "Title" All lengths > 2, so capitalize FINAL RESULT Transformed Words: "Capitalize" "The" "Title" Join with spaces Output: "Capitalize The Title" OK - Complete Key Insight: The problem requires checking word length to determine capitalization. Words with 1-2 characters become all lowercase (e.g., "a", "to" stay lowercase). Words with 3+ characters get title case (first letter uppercase, rest lowercase). This handles edge cases like "I" or "is" differently from longer words. TutorialsPoint - Capitalize the Title | Split and Process Approach
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