mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
54 lines
2.2 KiB
HTML
54 lines
2.2 KiB
HTML
<p>You are given a binary string <code>s</code> of length <code>n</code> and an integer <code>numOps</code>.</p>
|
|
|
|
<p>You are allowed to perform the following operation on <code>s</code> <strong>at most</strong> <code>numOps</code> times:</p>
|
|
|
|
<ul>
|
|
<li>Select any index <code>i</code> (where <code>0 <= i < n</code>) and <strong>flip</strong> <code>s[i]</code>. If <code>s[i] == '1'</code>, change <code>s[i]</code> to <code>'0'</code> and vice versa.</li>
|
|
</ul>
|
|
|
|
<p>You need to <strong>minimize</strong> the length of the <strong>longest</strong> <span data-keyword="substring-nonempty">substring</span> of <code>s</code> such that all the characters in the substring are <strong>identical</strong>.</p>
|
|
|
|
<p>Return the <strong>minimum</strong> length after the operations.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "000001", numOps = 1</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
|
|
|
<p><strong>Explanation:</strong> </p>
|
|
|
|
<p>By changing <code>s[2]</code> to <code>'1'</code>, <code>s</code> becomes <code>"001001"</code>. The longest substrings with identical characters are <code>s[0..1]</code> and <code>s[3..4]</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "0000", numOps = 2</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
|
|
|
<p><strong>Explanation:</strong> </p>
|
|
|
|
<p>By changing <code>s[0]</code> and <code>s[2]</code> to <code>'1'</code>, <code>s</code> becomes <code>"1010"</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "0101", numOps = 0</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= n == s.length <= 10<sup>5</sup></code></li>
|
|
<li><code>s</code> consists only of <code>'0'</code> and <code>'1'</code>.</li>
|
|
<li><code>0 <= numOps <= n</code></li>
|
|
</ul>
|