Skip to content

feat: add weekly contest 458 #4565

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 2 commits into from
Jul 13, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README.md
---

<!-- problem:start -->

# [3612. 用特殊操作处理字符串 I](https://leetcode.cn/problems/process-string-with-special-operations-i)

[English Version](/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个字符串 <code>s</code>,它由小写英文字母和特殊字符:<code>*</code>、<code>#</code> 和 <code>%</code> 组成。</p>

<p>请根据以下规则从左到右处理 <code>s</code>&nbsp;中的字符,构造一个新的字符串 <code>result</code>:</p>

<ul>
<li>如果字符是 <strong>小写</strong> 英文字母,则将其添加到 <code>result</code> 中。</li>
<li>字符 <code>'*'</code> 会&nbsp;<strong>删除</strong> <code>result</code> 中的最后一个字符(如果存在)。</li>
<li>字符 <code>'#'</code> 会&nbsp;<strong>复制&nbsp;</strong>当前的 <code>result</code> 并&nbsp;<strong>追加&nbsp;</strong>到其自身后面。</li>
<li>字符 <code>'%'</code> 会&nbsp;<strong>反转&nbsp;</strong>当前的 <code>result</code>。</li>
</ul>

<p>在处理完 <code>s</code> 中的所有字符后,返回最终的字符串 <code>result</code>。</p>

<p>&nbsp;</p>

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

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "a#b%*"</span></p>

<p><strong>输出:</strong> <span class="example-io">"ba"</span></p>

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

<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>s[i]</code></th>
<th style="border: 1px solid black;">操作</th>
<th style="border: 1px solid black;">当前 <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>'a'</code></td>
<td style="border: 1px solid black;">添加 <code>'a'</code></td>
<td style="border: 1px solid black;"><code>"a"</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>'#'</code></td>
<td style="border: 1px solid black;">复制 <code>result</code></td>
<td style="border: 1px solid black;"><code>"aa"</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>'b'</code></td>
<td style="border: 1px solid black;">添加 <code>'b'</code></td>
<td style="border: 1px solid black;"><code>"aab"</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>'%'</code></td>
<td style="border: 1px solid black;">反转 <code>result</code></td>
<td style="border: 1px solid black;"><code>"baa"</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;"><code>'*'</code></td>
<td style="border: 1px solid black;">删除最后一个字符</td>
<td style="border: 1px solid black;"><code>"ba"</code></td>
</tr>
</tbody>
</table>

<p>因此,最终的 <code>result</code> 是 <code>"ba"</code>。</p>
</div>

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

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">s = "z*#"</span></p>

<p><strong>输出:</strong> <span class="example-io">""</span></p>

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

<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>s[i]</code></th>
<th style="border: 1px solid black;">操作</th>
<th style="border: 1px solid black;">当前 <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>'z'</code></td>
<td style="border: 1px solid black;">添加 <code>'z'</code></td>
<td style="border: 1px solid black;"><code>"z"</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>'*'</code></td>
<td style="border: 1px solid black;">删除最后一个字符</td>
<td style="border: 1px solid black;"><code>""</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>'#'</code></td>
<td style="border: 1px solid black;">复制字符串</td>
<td style="border: 1px solid black;"><code>""</code></td>
</tr>
</tbody>
</table>

<p>因此,最终的 <code>result</code> 是 <code>""</code>。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 20</code></li>
<li><code>s</code> 只包含小写英文字母和特殊字符 <code>*</code>、<code>#</code> 和 <code>%</code>。</li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README_EN.md
---

<!-- problem:start -->

# [3612. Process String with Special Operations I](https://leetcode.com/problems/process-string-with-special-operations-i)

[中文文档](/solution/3600-3699/3612.Process%20String%20with%20Special%20Operations%20I/README.md)

## Description

<!-- description:start -->

<p>You are given a string <code>s</code> consisting of lowercase English letters and the special characters: <code>*</code>, <code>#</code>, and <code>%</code>.</p>

<p>Build a new string <code>result</code> by processing <code>s</code> according to the following rules from left to right:</p>

<ul>
<li>If the letter is a <strong>lowercase</strong> English letter append it to <code>result</code>.</li>
<li>A <code>&#39;*&#39;</code> <strong>removes</strong> the last character from <code>result</code>, if it exists.</li>
<li>A <code>&#39;#&#39;</code> <strong>duplicates</strong> the current <code>result</code> and <strong>appends</strong> it to itself.</li>
<li>A <code>&#39;%&#39;</code> <strong>reverses</strong> the current <code>result</code>.</li>
</ul>

<p>Return the final string <code>result</code> after processing all characters in <code>s</code>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;a#b%*&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">&quot;ba&quot;</span></p>

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

<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;a&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;a&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;aa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;b&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;aab&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">3</td>
<td style="border: 1px solid black;"><code>&#39;%&#39;</code></td>
<td style="border: 1px solid black;">Reverse <code>result</code></td>
<td style="border: 1px solid black;"><code>&quot;baa&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">4</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;ba&quot;</code></td>
</tr>
</tbody>
</table>

<p>Thus, the final <code>result</code> is <code>&quot;ba&quot;</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;z*#&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>

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

<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;"><code>i</code></th>
<th style="border: 1px solid black;"><code>s[i]</code></th>
<th style="border: 1px solid black;">Operation</th>
<th style="border: 1px solid black;">Current <code>result</code></th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;"><code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;">Append <code>&#39;z&#39;</code></td>
<td style="border: 1px solid black;"><code>&quot;z&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;"><code>&#39;*&#39;</code></td>
<td style="border: 1px solid black;">Remove the last character</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
<tr>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;"><code>&#39;#&#39;</code></td>
<td style="border: 1px solid black;">Duplicate the string</td>
<td style="border: 1px solid black;"><code>&quot;&quot;</code></td>
</tr>
</tbody>
</table>

<p>Thus, the final <code>result</code> is <code>&quot;&quot;</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 20</code></li>
<li><code>s</code> consists of only lowercase English letters and special characters <code>*</code>, <code>#</code>, and <code>%</code>.</li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python

```

#### Java

```java

```

#### C++

```cpp

```

#### Go

```go

```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading