First Letter Capitalization II - Problem
You work for a content management system that needs to properly format user-generated text. Your task is to implement a smart text capitalization system that transforms text according to professional publishing standards.
Given a database table user_content with user-generated text, you need to:
- Capitalize the first letter of each word and make remaining letters lowercase
- Handle hyphenated words specially: Both parts of hyphenated words should be capitalized (e.g.,
top-ratedโTop-Rated) - Preserve all spacing and formatting
The table structure:
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| content_id | int |
| content_text| varchar |
+-------------+---------+Return both the original and transformed text for comparison.
Input & Output
example_1.sql โ Basic Capitalization
$
Input:
user_content:
| content_id | content_text |
|------------|-------------|
| 1 | "hello world" |
โบ
Output:
| content_id | content_text | capitalized_text |
|------------|--------------|------------------|
| 1 | "hello world" | "Hello World" |
๐ก Note:
Each word's first letter is capitalized: 'hello world' becomes 'Hello World'. The space is preserved between words.
example_2.sql โ Hyphenated Words
$
Input:
user_content:
| content_id | content_text |
|------------|-------------|
| 2 | "top-rated product" |
โบ
Output:
| content_id | content_text | capitalized_text |
|------------|--------------|------------------|
| 2 | "top-rated product" | "Top-Rated Product" |
๐ก Note:
Hyphenated words get both parts capitalized: 'top-rated' becomes 'Top-Rated', and 'product' becomes 'Product'.
example_3.sql โ Mixed Case Edge Cases
$
Input:
user_content:
| content_id | content_text |
|------------|-------------|
| 3 | "HTML-based WEB development" |
โบ
Output:
| content_id | content_text | capitalized_text |
|------------|--------------|------------------|
| 3 | "HTML-based WEB development" | "Html-Based Web Development" |
๐ก Note:
All words are normalized: 'HTML-based' becomes 'Html-Based', 'WEB' becomes 'Web', and 'development' becomes 'Development'.
Constraints
- 1 โค number of rows โค 104
- 1 โค length of content_text โค 1000
- content_text contains only English letters, spaces, and hyphens
- No leading or trailing spaces in individual words
- Hyphens only appear between letters (not at start/end of words)
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Scan the text to identify word boundaries and special characters
2
Pattern Detection
Find positions where capitalization should occur (start of words, after hyphens)
3
Transform Application
Apply capitalization rules while preserving original spacing and formatting
4
Result Assembly
Combine transformed characters into the final formatted text
Key Takeaway
๐ฏ Key Insight: Using regex patterns `(^|[ ])([a-z])` and `(-)([a-z])` allows SQL to identify and transform characters in a single pass, making this both efficient and database-optimized.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code