高级农民
- 积分
- 4195
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
3374. First Letter Capitalization II
Hard
Topics
SQL Schema
Pandas Schema
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-rated becomes Top-Rated and FR-ONT-end becomes Fr-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_text contains only English letters, spaces, and the characters \, @, -, /, ^, and ,.- # Write your MySQL query statement below
- WITH RECURSIVE build AS (
- SELECT
- content_id,
- content_text,
- 1 AS i,
- CASE
- WHEN SUBSTRING_INDEX(content_text, ' ', 1) NOT REGEXP '^[A-Za-z]' THEN 0
- WHEN SUBSTRING_INDEX(content_text, ' ', 1) REGEXP '^[A-Za-z]+(-[A-Za-z]+)+ THEN 2
- ELSE 1
- END AS m,
- CAST('' AS CHAR(1000)) AS res
- FROM user_content
- UNION ALL
- SELECT
- content_id,
- content_text,
- i + 1,
- CASE
- WHEN SUBSTRING(content_text, i, 1) <> ' ' THEN m -- still inside a word
- WHEN SUBSTRING_INDEX(SUBSTRING(content_text, i + 1), ' ', 1) NOT REGEXP '^[A-Za-z]' THEN 0
- WHEN SUBSTRING_INDEX(SUBSTRING(content_text, i + 1), ' ', 1) REGEXP '^[A-Za-z]+(-[A-Za-z]+)+ THEN 2
- ELSE 1
- END,
- CONCAT(
- res,
- CASE
- WHEN m = 0 THEN SUBSTRING(content_text, i, 1)
- WHEN i = 1
- OR SUBSTRING(content_text, i - 1, 1) = ' '
- OR (m = 2 AND SUBSTRING(content_text, i - 1, 1) = '-')
- THEN UPPER(SUBSTRING(content_text, i, 1))
- ELSE LOWER(SUBSTRING(content_text, i, 1))
- END
- )
- FROM build
- WHERE i <= CHAR_LENGTH(content_text)
- )
- SELECT
- content_id,
- content_text AS original_text,
- res AS converted_text
- FROM build
- WHERE i = CHAR_LENGTH(content_text) + 1
- ORDER BY content_id;
复制代码 |
|