|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3374\. First Letter Capitalization II |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +SQL Schema |
| 9 | + |
| 10 | +Table: `user_content` |
| 11 | + |
| 12 | + +-------------+---------+ |
| 13 | + | Column Name | Type | |
| 14 | + +-------------+---------+ |
| 15 | + | content_id | int | |
| 16 | + | content_text| varchar | |
| 17 | + +-------------+---------+ |
| 18 | +content_id is the unique key for this table. Each row contains a unique ID and the corresponding text content. |
| 19 | + |
| 20 | +Write a solution to transform the text in the `content_text` column by applying the following rules: |
| 21 | + |
| 22 | +* Convert the **first letter** of each word to **uppercase** and the **remaining** letters to **lowercase** |
| 23 | +* Special handling for words containing special characters: |
| 24 | + * For words connected with a hyphen `-`, **both parts** should be **capitalized** (**e.g.**, top-rated → Top-Rated) |
| 25 | +* All other **formatting** and **spacing** should remain **unchanged** |
| 26 | + |
| 27 | +Return _the result table that includes both the original `content_text` and the modified text following the above rules_. |
| 28 | + |
| 29 | +The result format is in the following example. |
| 30 | + |
| 31 | +**Example:** |
| 32 | + |
| 33 | +**Input:** |
| 34 | + |
| 35 | +user\_content table: |
| 36 | + |
| 37 | + +------------+---------------------------------+ |
| 38 | + | content_id | content_text | |
| 39 | + +------------+---------------------------------+ |
| 40 | + | 1 | hello world of SQL | |
| 41 | + | 2 | the QUICK-brown fox | |
| 42 | + | 3 | modern-day DATA science | |
| 43 | + | 4 | web-based FRONT-end development | |
| 44 | + +------------+---------------------------------+ |
| 45 | + |
| 46 | +**Output:** |
| 47 | + |
| 48 | + +------------+---------------------------------+---------------------------------+ |
| 49 | + | content_id | original_text | converted_text | |
| 50 | + +------------+---------------------------------+---------------------------------+ |
| 51 | + | 1 | hello world of SQL | Hello World Of Sql | |
| 52 | + | 2 | the QUICK-brown fox | The Quick-Brown Fox | |
| 53 | + | 3 | modern-day DATA science | Modern-Day Data Science | |
| 54 | + | 4 | web-based FRONT-end development | Web-Based Front-End Development | |
| 55 | + +------------+---------------------------------+---------------------------------+ |
| 56 | + |
| 57 | +**Explanation:** |
| 58 | + |
| 59 | +* For content\_id = 1: |
| 60 | + * Each word's first letter is capitalized: "Hello World Of Sql" |
| 61 | +* For content\_id = 2: |
| 62 | + * Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown" |
| 63 | + * Other words follow normal capitalization rules |
| 64 | +* For content\_id = 3: |
| 65 | + * Hyphenated word "modern-day" becomes "Modern-Day" |
| 66 | + * "DATA" is converted to "Data" |
| 67 | +* For content\_id = 4: |
| 68 | + * Contains two hyphenated words: "web-based" → "Web-Based" |
| 69 | + * And "FRONT-end" → "Front-End" |
| 70 | + |
| 71 | +## Solution |
| 72 | + |
| 73 | +```python |
| 74 | +import pandas as pd |
| 75 | + |
| 76 | +def capitalize_content(user_content): |
| 77 | + user_content['converted_text'] = (user_content.content_text.apply(lambda x: x.title())) |
| 78 | + return user_content.rename(columns={'content_text': 'original_text'}) |
| 79 | +``` |
0 commit comments