1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-04 06:51:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/用特殊操作处理字符串 I(English) [process-string-with-special-operations-i].html
2025-07-17 00:14:36 +08:00

120 lines
4.7 KiB
HTML

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