|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Hard |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md |
| 5 | +tags: |
| 6 | + - Database |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3374. First Letter Capitalization II](https://leetcode.com/problems/first-letter-capitalization-ii) |
| 12 | + |
| 13 | +[中文文档](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>user_content</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+---------+ |
| 25 | +| content_id | int | |
| 26 | +| content_text| varchar | |
| 27 | ++-------------+---------+ |
| 28 | +content_id is the unique key for this table. |
| 29 | +Each row contains a unique ID and the corresponding text content. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li>Convert the <strong>first letter</strong> of each word to <strong>uppercase</strong> and the <strong>remaining</strong> letters to <strong>lowercase</strong></li> |
| 36 | + <li>Special handling for words containing special characters: |
| 37 | + <ul> |
| 38 | + <li>For words connected with a hyphen <code>-</code>, <strong>both parts</strong> should be <strong>capitalized</strong> (<strong>e.g.</strong>, top-rated → Top-Rated)</li> |
| 39 | + </ul> |
| 40 | + </li> |
| 41 | + <li>All other <strong>formatting</strong> and <strong>spacing</strong> should remain <strong>unchanged</strong></li> |
| 42 | +</ul> |
| 43 | + |
| 44 | +<p>Return <em>the result table that includes both the original <code>content_text</code> and the modified text following the above rules</em>.</p> |
| 45 | + |
| 46 | +<p>The result format is in the following example.</p> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong class="example">Example:</strong></p> |
| 50 | + |
| 51 | +<div class="example-block"> |
| 52 | +<p><strong>Input:</strong></p> |
| 53 | + |
| 54 | +<p>user_content table:</p> |
| 55 | + |
| 56 | +<pre class="example-io"> |
| 57 | ++------------+---------------------------------+ |
| 58 | +| content_id | content_text | |
| 59 | ++------------+---------------------------------+ |
| 60 | +| 1 | hello world of SQL | |
| 61 | +| 2 | the QUICK-brown fox | |
| 62 | +| 3 | modern-day DATA science | |
| 63 | +| 4 | web-based FRONT-end development | |
| 64 | ++------------+---------------------------------+ |
| 65 | +</pre> |
| 66 | + |
| 67 | +<p><strong>Output:</strong></p> |
| 68 | + |
| 69 | +<pre class="example-io"> |
| 70 | ++------------+---------------------------------+---------------------------------+ |
| 71 | +| content_id | original_text | converted_text | |
| 72 | ++------------+---------------------------------+---------------------------------+ |
| 73 | +| 1 | hello world of SQL | Hello World Of Sql | |
| 74 | +| 2 | the QUICK-brown fox | The Quick-Brown Fox | |
| 75 | +| 3 | modern-day DATA science | Modern-Day Data Science | |
| 76 | +| 4 | web-based FRONT-end development | Web-Based Front-End Development | |
| 77 | ++------------+---------------------------------+---------------------------------+ |
| 78 | +</pre> |
| 79 | + |
| 80 | +<p><strong>Explanation:</strong></p> |
| 81 | + |
| 82 | +<ul> |
| 83 | + <li>For content_id = 1: |
| 84 | + <ul> |
| 85 | + <li>Each word's first letter is capitalized: "Hello World Of Sql"</li> |
| 86 | + </ul> |
| 87 | + </li> |
| 88 | + <li>For content_id = 2: |
| 89 | + <ul> |
| 90 | + <li>Contains the hyphenated word "QUICK-brown" which becomes "Quick-Brown"</li> |
| 91 | + <li>Other words follow normal capitalization rules</li> |
| 92 | + </ul> |
| 93 | + </li> |
| 94 | + <li>For content_id = 3: |
| 95 | + <ul> |
| 96 | + <li>Hyphenated word "modern-day" becomes "Modern-Day"</li> |
| 97 | + <li>"DATA" is converted to "Data"</li> |
| 98 | + </ul> |
| 99 | + </li> |
| 100 | + <li>For content_id = 4: |
| 101 | + <ul> |
| 102 | + <li>Contains two hyphenated words: "web-based" → "Web-Based"</li> |
| 103 | + <li>And "FRONT-end" → "Front-End"</li> |
| 104 | + </ul> |
| 105 | + </li> |
| 106 | +</ul> |
| 107 | +</div> |
| 108 | + |
| 109 | +<!-- description:end --> |
| 110 | + |
| 111 | +## Solutions |
| 112 | + |
| 113 | +<!-- solution:start --> |
| 114 | + |
| 115 | +### Solution 1 |
| 116 | + |
| 117 | +<!-- tabs:start --> |
| 118 | + |
| 119 | +#### Pandas |
| 120 | + |
| 121 | +```python |
| 122 | +import pandas as pd |
| 123 | + |
| 124 | + |
| 125 | +def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame: |
| 126 | + def convert_text(text: str) -> str: |
| 127 | + return " ".join( |
| 128 | + ( |
| 129 | + "-".join([part.capitalize() for part in word.split("-")]) |
| 130 | + if "-" in word |
| 131 | + else word.capitalize() |
| 132 | + ) |
| 133 | + for word in text.split(" ") |
| 134 | + ) |
| 135 | + |
| 136 | + user_content["converted_text"] = user_content["content_text"].apply(convert_text) |
| 137 | + return user_content.rename(columns={"content_text": "original_text"})[ |
| 138 | + ["content_id", "original_text", "converted_text"] |
| 139 | + ] |
| 140 | +``` |
| 141 | + |
| 142 | +<!-- tabs:end --> |
| 143 | + |
| 144 | +<!-- solution:end --> |
| 145 | + |
| 146 | +<!-- problem:end --> |
0 commit comments