mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-24 14:28:56 +08:00
update
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
<p>You are given a string <code>s</code> consisting of <code>'('</code> and <code>')'</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>A <strong>string</strong> is <strong>k-balanced</strong> if it is <strong>exactly</strong> <code>k</code> <strong>consecutive</strong> <code>'('</code> followed by <code>k</code> <strong>consecutive</strong> <code>')'</code>, i.e., <code>'(' * k + ')' * k</code>.</p>
|
||||
|
||||
<p>For example, if <code>k = 3</code>, k-balanced is <code>"((()))"</code>.</p>
|
||||
|
||||
<p>You must <strong>repeatedly</strong> remove all <strong>non-overlapping k-balanced <span data-keyword="substring-nonempty">substrings</span></strong> from <code>s</code>, and then join the remaining parts. Continue this process until no k-balanced <strong>substring</strong> exists.</p>
|
||||
|
||||
<p>Return the final string after all possible removals.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "(())", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">""</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>k-balanced substring is <code>"()"</code></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Step</th>
|
||||
<th style="border: 1px solid black;">Current <code>s</code></th>
|
||||
<th style="border: 1px solid black;"><code>k-balanced</code></th>
|
||||
<th style="border: 1px solid black;">Result <code>s</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<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>(<s><strong>()</strong></s>)</code></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;"><s><strong><code>()</code></strong></s></td>
|
||||
<td style="border: 1px solid black;">Empty</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Thus, the final string is <code>""</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "(()(", k = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"(("</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>k-balanced substring is <code>"()"</code></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Step</th>
|
||||
<th style="border: 1px solid black;">Current <code>s</code></th>
|
||||
<th style="border: 1px solid black;"><code>k-balanced</code></th>
|
||||
<th style="border: 1px solid black;">Result <code>s</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<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>(<s><strong>()</strong></s>(</code></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>Thus, the final string is <code>"(("</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "((()))()()()", k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">"()()()"</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>k-balanced substring is <code>"((()))"</code></p>
|
||||
|
||||
<table style="border: 1px solid black;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="border: 1px solid black;">Step</th>
|
||||
<th style="border: 1px solid black;">Current <code>s</code></th>
|
||||
<th style="border: 1px solid black;"><code>k-balanced</code></th>
|
||||
<th style="border: 1px solid black;">Result <code>s</code></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<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><s><strong>((()))</strong></s>()()()</code></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>Thus, the final string is <code>"()()()"</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists only of <code>'('</code> and <code>')'</code>.</li>
|
||||
<li><code>1 <= k <= s.length / 2</code></li>
|
||||
</ul>
|
||||
Reference in New Issue
Block a user