3374. First Letter Capitalization II LeetCode Solution
In this guide, you will get 3374. First Letter Capitalization II LeetCode Solution with the best time and space complexity. The solution to First Letter Capitalization II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- First Letter Capitalization II solution in C++
- First Letter Capitalization II solution in Java
- First Letter Capitalization II solution in Python
- Additional Resources

Problem Statement of First Letter Capitalization II
Table: user_content
+————-+———+
| Column Name | Type |
+————-+———+
| content_id | int |
| content_text| varchar |
+————-+———+
content_id is the unique key for this table.
Each row contains a unique ID and the corresponding text content.
Write a solution to transform the text in the content_text column by applying the following rules:
Convert the first letter of each word to uppercase and the remaining letters to lowercase
Special handling for words containing special characters:
For words connected with a hyphen -, both parts should be capitalized (e.g., top-rated → Top-Rated)
All other formatting and spacing should remain unchanged
Return the result table that includes both the original content_text and the modified text following the above rules.
The result format is in the following example.
Example:
Input:
user_content table:
+————+———————————+
| content_id | content_text |
+————+———————————+
| 1 | hello world of SQL |
| 2 | the QUICK-brown fox |
| 3 | modern-day DATA science |
| 4 | web-based FRONT-end development |
+————+———————————+
Output:
+————+———————————+———————————+
| content_id | original_text | converted_text |
+————+———————————+———————————+
| 1 | hello world of SQL | Hello World Of Sql |
| 2 | the QUICK-brown fox | The Quick-Brown Fox |
| 3 | modern-day DATA science | Modern-Day Data Science |
| 4 | web-based FRONT-end development | Web-Based Front-End Development |
+————+———————————+———————————+
Explanation:
For content_id = 1:
Each word’s first letter is capitalized: “Hello World Of Sql”
For content_id = 2:
Contains the hyphenated word “QUICK-brown” which becomes “Quick-Brown”
Other words follow normal capitalization rules
For content_id = 3:
Hyphenated word “modern-day” becomes “Modern-Day”
“DATA” is converted to “Data”
For content_id = 4:
Contains two hyphenated words: “web-based” → “Web-Based”
And “FRONT-end” → “Front-End”
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
3374. First Letter Capitalization II LeetCode Solution in C++
WITH RECURSIVE
Words AS (
-- Extract the first word and assign a token index.
SELECT
content_id,
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
SUBSTRING(
content_text,
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
) AS remaining_text,
AS token_index
FROM user_content
UNION ALL
-- Recursively extract the next word and increment the token index.
SELECT
content_id,
SUBSTRING_INDEX(remaining_text, ' ', 1) AS word,
SUBSTRING(
remaining_text,
LENGTH(SUBSTRING_INDEX(remaining_text, ' ', 1)) + 2
) AS remaining_text,
token_index + 1 AS token_index
FROM Words
WHERE remaining_text != ''
),
Converted AS (
-- Combine the words back in original order with proper capitalization.
SELECT
content_id,
GROUP_CONCAT(
IF(
word LIKE '%-%',
CONCAT(
UPPER(SUBSTRING(word, 1, 1)),
LOWER(SUBSTRING(word, 2, LOCATE('-', word) - 2)),
'-',
UPPER(SUBSTRING(SUBSTRING_INDEX(word, '-', -1), 1, 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(word, '-', -1), 2))
),
CONCAT(
UPPER(SUBSTRING(word, 1, 1)),
LOWER(SUBSTRING(word, 2))
)
)
ORDER BY token_index SEPARATOR ' '
) AS converted_text
FROM Words
GROUP BY 1
)
SELECT
UserContent.content_id,
UserContent.content_text AS original_text,
Converted.converted_text
FROM user_content AS UserContent
INNER JOIN Converted
USING (content_id);
/* code provided by PROGIEZ */
3374. First Letter Capitalization II LeetCode Solution in Java
N/A
// code provided by PROGIEZ
3374. First Letter Capitalization II LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.