Skip to content

feat: add solutions to lc problem: No.3374 #3831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions solution/0200-0299/0277.Find the Celebrity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ tags:

<p>现在你想要确认这个 “名人” 是谁,或者确定这里没有&nbsp;“名人”。而你唯一能做的就是问诸如 “A&nbsp;你好呀,请问你认不认识&nbsp;B呀?”&nbsp;的问题,以确定 A 是否认识 B。你需要在(渐近意义上)尽可能少的问题内来确定这位 “名人” 是谁(或者确定这里没有 “名人”)。</p>

<p>在本题中,你可以使用辅助函数&nbsp;<code>bool knows(a, b)</code>&nbsp;获取到 A&nbsp;是否认识 B。请你来实现一个函数&nbsp;<code>int findCelebrity(n)</code>。</p>
<p>给定整数&nbsp;<code>n</code>&nbsp;和一个辅助函数&nbsp;<code>bool knows(a, b)</code>&nbsp;用来获取&nbsp;<code>a</code> 是否认识&nbsp;<code>b</code>。实现一个函数&nbsp;<code>int findCelebrity(n)</code>。派对最多只会有一个 “名人” 参加。</p>

<p>派对最多只会有一个 “名人” 参加。若&nbsp;“名人” 存在,请返回他/她的编号;若&nbsp;“名人”&nbsp;不存在,请返回&nbsp;<code>-1</code>。</p>
<p>若&nbsp;“名人” 存在,请返回他/她的编号;若&nbsp;“名人”&nbsp;不存在,请返回&nbsp;<code>-1</code>。</p>

<p><strong>注意</strong>&nbsp;<code>n x n</code>&nbsp;的二维数组&nbsp;<code>graph</code>&nbsp;给定的输入并不是&nbsp;<strong>直接</strong>&nbsp;提供给你的,而是&nbsp;<strong>只能</strong>&nbsp;通过辅助函数 <code>knows</code>&nbsp;获取。<code>graph[i][j] == 1</code>&nbsp;表示&nbsp;<code>i</code> 认识 <code>j</code>,而&nbsp;<code>graph[i][j] == 0</code>&nbsp;表示&nbsp;<code>j</code>&nbsp;不认识&nbsp;<code>i</code>。</p>

<p>&nbsp;</p>

Expand Down
6 changes: 3 additions & 3 deletions solution/3300-3399/3368.First Letter Capitalization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ content_id 是这张表的唯一主键。
+------------+-----------------------------------+
| content_id | content_text |
+------------+-----------------------------------+
| 1 | hello world of SQL |
| 1 | hello world of Sql |
| 2 | the QUICK brown fox |
| 3 | data science AND machine learning |
| 4 | TOP rated programming BOOKS |
Expand All @@ -69,7 +69,7 @@ content_id 是这张表的唯一主键。
+------------+-----------------------------------+-----------------------------------+
| content_id | original_text | converted_text |
+------------+-----------------------------------+-----------------------------------+
| 1 | hello world of SQL | Hello World Of SQL |
| 1 | hello world of Sql | Hello World Of Sql |
| 2 | the QUICK brown fox | The Quick Brown Fox |
| 3 | data science AND machine learning | Data Science And Machine Learning |
| 4 | TOP rated programming BOOKS | Top Rated Programming Books |
Expand All @@ -81,7 +81,7 @@ content_id 是这张表的唯一主键。
<ul>
<li>对于 content_id = 1:
<ul>
<li>每个单词的首字母都已经大写:Hello World Of SQL</li>
<li>每个单词的首字母都已经大写:Hello World Of Sql</li>
</ul>
</li>
<li>对于 content_id = 2:
Expand Down
147 changes: 147 additions & 0 deletions solution/3300-3399/3374.First Letter Capitalization II/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md
tags:
- 数据库
---

<!-- problem:start -->

# [3374. 首字母大写 II](https://leetcode.cn/problems/first-letter-capitalization-ii)

[English Version](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md)

## 题目描述

<!-- description:start -->

<p>表:<code>user_content</code></p>

<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| content_id | int |
| content_text| varchar |
+-------------+---------+
content_id 是这张表的唯一主键。
每一行包含一个不同的 ID 以及对应的文本内容。
</pre>

<p>编写一个解决方案来根据下面的规则来转换&nbsp;<code>content_text</code>&nbsp;列中的文本:</p>

<ul>
<li>将每个单词的 <strong>第一个字母</strong>&nbsp;转换为 <strong>大写</strong>,其余字母 <strong>保持小写</strong>。</li>
<li>特殊处理包含特殊字符的单词:
<ul>
<li>对于用短横&nbsp;<code>-</code>&nbsp;连接的词语,<strong>两个部份</strong>&nbsp;都应该&nbsp;<strong>大写</strong>(<strong>例如</strong>,top-rated&nbsp;→ Top-Rated)</li>
</ul>
</li>
<li>所有其他 <strong>格式</strong> 和 <strong>空格</strong> 应保持 <strong>不变</strong></li>
</ul>

<p>返回结果表同时包含原始的&nbsp;<code>content_text</code> 以及根据上述规则修改后的文本。</p>

<p>结果格式如下例所示。</p>

<p>&nbsp;</p>

<p><strong class="example">示例:</strong></p>

<div class="example-block">
<p><strong>输入:</strong></p>

<p>user_content 表:</p>

<pre class="example-io">
+------------+---------------------------------+
| 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 |
+------------+---------------------------------+
</pre>

<p><strong>输出:</strong></p>

<pre class="example-io">
+------------+---------------------------------+---------------------------------+
| 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 |
+------------+---------------------------------+---------------------------------+
</pre>

<p><strong>解释:</strong></p>

<ul>
<li>对于 content_id = 1:
<ul>
<li>每个单词的首字母都是大写的:"Hello World Of Sql"</li>
</ul>
</li>
<li>对于 content_id = 2:
<ul>
<li>包含的连字符词 "QUICK-brown" 变为 "Quick-Brown"</li>
<li>其它单词遵循普通的首字母大写规则</li>
</ul>
</li>
<li>对于 content_id = 3:
<ul>
<li>连字符词 "modern-day" 变为 "Modern-Day"</li>
<li>"DATA" 转换为 "Data"</li>
</ul>
</li>
<li>对于 content_id = 4:
<ul>
<li>包含两个连字符词:"web-based" → "Web-Based"</li>
<li>以及 "FRONT-end" → "Front-End"</li>
</ul>
</li>
</ul>
</div>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Pandas

```python
import pandas as pd


def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
def convert_text(text: str) -> str:
return " ".join(
(
"-".join([part.capitalize() for part in word.split("-")])
if "-" in word
else word.capitalize()
)
for word in text.split(" ")
)

user_content["converted_text"] = user_content["content_text"].apply(convert_text)
return user_content.rename(columns={"content_text": "original_text"})[
["content_id", "original_text", "converted_text"]
]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
146 changes: 146 additions & 0 deletions solution/3300-3399/3374.First Letter Capitalization II/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
---
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md
tags:
- Database
---

<!-- problem:start -->

# [3374. First Letter Capitalization II](https://leetcode.com/problems/first-letter-capitalization-ii)

[中文文档](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md)

## Description

<!-- description:start -->

<p>Table: <code>user_content</code></p>

<pre>
+-------------+---------+
| 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.
</pre>

<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p>

<ul>
<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>
<li>Special handling for words containing special characters:
<ul>
<li>For words connected with a hyphen <code>-</code>, <strong>both parts</strong> should be <strong>capitalized</strong> (<strong>e.g.</strong>, top-rated&nbsp;&rarr; Top-Rated)</li>
</ul>
</li>
<li>All other <strong>formatting</strong> and <strong>spacing</strong> should remain <strong>unchanged</strong></li>
</ul>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>

<div class="example-block">
<p><strong>Input:</strong></p>

<p>user_content table:</p>

<pre class="example-io">
+------------+---------------------------------+
| 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 |
+------------+---------------------------------+
</pre>

<p><strong>Output:</strong></p>

<pre class="example-io">
+------------+---------------------------------+---------------------------------+
| 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 |
+------------+---------------------------------+---------------------------------+
</pre>

<p><strong>Explanation:</strong></p>

<ul>
<li>For content_id = 1:
<ul>
<li>Each word&#39;s first letter is capitalized: &quot;Hello World Of Sql&quot;</li>
</ul>
</li>
<li>For content_id = 2:
<ul>
<li>Contains the hyphenated word &quot;QUICK-brown&quot; which becomes &quot;Quick-Brown&quot;</li>
<li>Other words follow normal capitalization rules</li>
</ul>
</li>
<li>For content_id = 3:
<ul>
<li>Hyphenated word &quot;modern-day&quot; becomes &quot;Modern-Day&quot;</li>
<li>&quot;DATA&quot; is converted to &quot;Data&quot;</li>
</ul>
</li>
<li>For content_id = 4:
<ul>
<li>Contains two hyphenated words: &quot;web-based&quot; &rarr; &quot;Web-Based&quot;</li>
<li>And &quot;FRONT-end&quot; &rarr; &quot;Front-End&quot;</li>
</ul>
</li>
</ul>
</div>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Pandas

```python
import pandas as pd


def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
def convert_text(text: str) -> str:
return " ".join(
(
"-".join([part.capitalize() for part in word.split("-")])
if "-" in word
else word.capitalize()
)
for word in text.split(" ")
)

user_content["converted_text"] = user_content["content_text"].apply(convert_text)
return user_content.rename(columns={"content_text": "original_text"})[
["content_id", "original_text", "converted_text"]
]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
18 changes: 18 additions & 0 deletions solution/3300-3399/3374.First Letter Capitalization II/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pandas as pd


def capitalize_content(user_content: pd.DataFrame) -> pd.DataFrame:
def convert_text(text: str) -> str:
return " ".join(
(
"-".join([part.capitalize() for part in word.split("-")])
if "-" in word
else word.capitalize()
)
for word in text.split(" ")
)

user_content["converted_text"] = user_content["content_text"].apply(convert_text)
return user_content.rename(columns={"content_text": "original_text"})[
["content_id", "original_text", "converted_text"]
]
1 change: 1 addition & 0 deletions solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
| 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 |
| 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |
| 3374 | [首字母大写 II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) | | 困难 | |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/DATABASE_README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3338 | [Second Highest Salary II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README_EN.md) | `Database` | Medium | 🔒 |
| 3358 | [Books with NULL Ratings](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README_EN.md) | `Database` | Easy | 🔒 |
| 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md) | | Hard | 🔒 |
| 3374 | [First Letter Capitalization II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md) | | Hard | |

## Copyright

Expand Down
1 change: 1 addition & 0 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3383,6 +3383,7 @@
| 3371 | [识别数组中的最大异常值](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README.md) | | 中等 | 第 426 场周赛 |
| 3372 | [连接两棵树后最大目标节点数目 I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README.md) | | 中等 | 第 426 场周赛 |
| 3373 | [连接两棵树后最大目标节点数目 II](/solution/3300-3399/3373.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20II/README.md) | | 困难 | 第 426 场周赛 |
| 3374 | [首字母大写 II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README.md) | | 困难 | |

## 版权

Expand Down
1 change: 1 addition & 0 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3381,6 +3381,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3371 | [Identify the Largest Outlier in an Array](/solution/3300-3399/3371.Identify%20the%20Largest%20Outlier%20in%20an%20Array/README_EN.md) | | Medium | Weekly Contest 426 |
| 3372 | [Maximize the Number of Target Nodes After Connecting Trees I](/solution/3300-3399/3372.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20I/README_EN.md) | | Medium | Weekly Contest 426 |
| 3373 | [Maximize the Number of Target Nodes After Connecting Trees II](/solution/3300-3399/3373.Maximize%20the%20Number%20of%20Target%20Nodes%20After%20Connecting%20Trees%20II/README_EN.md) | | Hard | Weekly Contest 426 |
| 3374 | [First Letter Capitalization II](/solution/3300-3399/3374.First%20Letter%20Capitalization%20II/README_EN.md) | | Hard | |

## Copyright

Expand Down