3374. First Letter Capitalization II
DifficultyHard
Description
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.
A word is a maximal non-empty sequence of characters that does not contain a space.
Write a solution to transform the text in the content_text column by applying the following rules to each word:
- If the word starts with a character that is not an English letter, leave the entire word unchanged.
- Otherwise, if the word consists of two or more non-empty parts of English letters connected by hyphens
-, convert the first letter of each part to uppercase and the remaining letters of each part to lowercase. For example,top-ratedbecomesTop-RatedandFR-ONT-endbecomesFr-Ont-End. - Otherwise, convert the first letter of the word to uppercase and all remaining English letters to lowercase. Any special characters remain unchanged.
All other formatting and spacing must 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:
- The hyphenated word "QUICK-brown" becomes "Quick-Brown".
- Other words follow the normal capitalization rules.
- For content_id = 3:
- The hyphenated word "modern-day" becomes "Modern-Day".
- "DATA" is converted to "Data".
- For content_id = 4:
- "web-based" becomes "Web-Based".
- "FRONT-end" becomes "Front-End".
Constraints:
content_textcontains only English letters, spaces, and the characters\,@,-,/,^, and,.
Solutions
Solution 1
Thinking
Besides capitalizing words as in part I, each hyphen-separated piece must be capitalized on its own.
Split on spaces; if a token contains -, split again and \(\textit{capitalize}\) each piece.
Thus foo-bar becomes Foo-Bar, and the rest follows part I.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |