First Letter Capitalization - Problem

You are working as a data engineer for a content management system that needs to standardize text formatting across their database. Your task is to create a title case transformation function that capitalizes the first letter of each word while converting all other letters to lowercase.

Given a table user_content with columns content_id and content_text, you need to:

  • Capitalize the first letter of each word
  • Convert all other letters to lowercase
  • Preserve all existing spaces exactly as they are
  • Return both the original text and the transformed text

The challenge lies in handling various edge cases like multiple consecutive spaces, mixed-case words, and all-caps text. This is commonly needed for cleaning user-generated content, standardizing titles, or preparing text for display.

Note: The input contains only alphabetic characters and spaces - no special characters or punctuation.

Input & Output

example_1.py โ€” Basic Mixed Case
$ Input: content_text = "hello world of SQL"
โ€บ Output: "Hello World Of Sql"
๐Ÿ’ก Note: Each word's first letter is capitalized: 'hello' โ†’ 'Hello', 'world' โ†’ 'World', 'of' โ†’ 'Of', 'SQL' โ†’ 'Sql'. All other letters are converted to lowercase.
example_2.py โ€” All Caps Conversion
$ Input: content_text = "the QUICK brown fox"
โ€บ Output: "The Quick Brown Fox"
๐Ÿ’ก Note: The all-caps word 'QUICK' is converted to title case 'Quick'. Mixed case words like 'the' and 'brown' are properly capitalized, while 'fox' gets its first letter capitalized.
example_3.py โ€” Multiple Spaces
$ Input: content_text = "data science AND"
โ€บ Output: "Data Science And"
๐Ÿ’ก Note: Multiple consecutive spaces are preserved exactly as they appear. Each word boundary is correctly identified regardless of space count: 'data' โ†’ 'Data', 'science' โ†’ 'Science', 'AND' โ†’ 'And'.

Constraints

  • 1 โ‰ค content_text.length โ‰ค 1000
  • content_text contains only alphabetic characters and spaces
  • No leading or trailing spaces guaranteed, but may exist
  • Multiple consecutive spaces may exist and must be preserved

Visualization

Tap to expand
State Machine: Title Case TransformationWORDSTARTWITHINWORDLetter: Capitalizeโ†’ word_start = falseSpace: Preserveโ†’ word_start = trueLetter: Lowercase(stay in state)Example Walkthrough: "hello WORLD"hSTARTโ†’ HeWITHINโ†’ elโ†’ llโ†’ loโ†’ o SPACEโ†’ WSTARTโ†’ WOWITHINโ†’ oResult: "Hello World"
Understanding the Visualization
1
Initialize State
Start with word_start = true, ready to capitalize the first character
2
Process Characters
For each character: if space, preserve and set word_start = true; if letter and word_start, capitalize and set word_start = false; if letter and not word_start, make lowercase
3
Build Result
Append each processed character to build the final title-case string
4
Complete
Return the transformed string with proper title case formatting
Key Takeaway
๐ŸŽฏ Key Insight: By treating this as a simple two-state machine, we can process the entire string in a single pass while correctly handling all capitalization rules and preserving spaces.
Asked in
Meta 35 Google 28 Microsoft 22 Amazon 18
24.7K Views
Medium Frequency
~15 min Avg. Time
847 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