mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 19:31:42 +08:00
批量更新数据
This commit is contained in:
@@ -1,38 +1,67 @@
|
||||
<p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p>
|
||||
<p>Given a string <code>s</code>, you have two types of operation:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li>
|
||||
</ul>
|
||||
<ol>
|
||||
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if exists).</li>
|
||||
<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if exists).</li>
|
||||
</ol>
|
||||
|
||||
<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p>
|
||||
<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operations zero or more times.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p>
|
||||
<p>Return an integer denoting the length of the <strong>minimized</strong> string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, s is "aaabc". We can start by selecting the character 'a' at index 1. We then remove the closest 'a' to the left of index 1, which is at index 0, and the closest 'a' to the right of index 1, which is at index 2. After this operation, the string becomes "abc". Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "aaabc"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>Operation 2: we choose <code>i = 1</code> so <code>c</code> is 'a', then we remove <code>s[2]</code> as it is closest 'a' character to the right of <code>s[1]</code>.<br />
|
||||
<code>s</code> becomes "aabc" after this.</li>
|
||||
<li>Operation 1: we choose <code>i = 1</code> so <code>c</code> is 'a', then we remove <code>s[0]</code> as it is closest 'a' character to the left of <code>s[1]</code>.<br />
|
||||
<code>s</code> becomes "abc" after this.</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbbd"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> For this we can start with character 'b' at index 1. There is no occurrence of 'b' to the left of index 1, but there is one to the right at index 2, so we delete the 'b' at index 2. The string becomes "cbd" and further operations will leave it unchanged. Hence, the minimized length is 3.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "cbbd"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>Operation 1: we choose <code>i = 2</code> so <code>c</code> is 'b', then we remove <code>s[1]</code> as it is closest 'b' character to the left of <code>s[1]</code>.<br />
|
||||
<code>s</code> becomes "cbd" after this.</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "dddaaa"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this, we can start with the character 'd' at index 1. The closest occurrence of a 'd' to its left is at index 0, and the closest occurrence of a 'd' to its right is at index 2. We delete both index 0 and 2, so the string becomes "daaa". In the new string, we can select the character 'a' at index 2. The closest occurrence of an 'a' to its left is at index 1, and the closest occurrence of an 'a' to its right is at index 3. We delete both of them, and the string becomes "da". We cannot minimize this further, so the minimized length is 2.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "baadccab"</span></p>
|
||||
|
||||
<div class="notranslate" style="all: initial;"> </div>
|
||||
<p><strong>Output:</strong> 4</p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>Operation 1: we choose <code>i = 6</code> so <code>c</code> is 'a', then we remove <code>s[2]</code> as it is closest 'a' character to the left of <code>s[6]</code>.<br />
|
||||
<code>s</code> becomes "badccab" after this.</li>
|
||||
<li>Operation 2: we choose <code>i = 0</code> so <code>c</code> is 'b', then we remove <code>s[6]</code> as it is closest 'b' character to the right of <code>s[0]</code>.<br />
|
||||
<code>s</code> becomes "badcca" fter this.</li>
|
||||
<li>Operation 2: we choose <code>i = 3</code> so <code>c</code> is 'c', then we remove <code>s[4]</code> as it is closest 'c' character to the right of <code>s[3]</code>.<br />
|
||||
<code>s</code> becomes "badca" after this.</li>
|
||||
<li>Operation 1: we choose <code>i = 4</code> so <code>c</code> is 'a', then we remove <code>s[1]</code> as it is closest 'a' character to the left of <code>s[4]</code>.<br />
|
||||
<code>s</code> becomes "bdca" after this.</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
Reference in New Issue
Block a user