mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-12-20 19:03:47 +08:00
220 lines
12 KiB
HTML
220 lines
12 KiB
HTML
<p>You are given a string <code>s</code> of length <code>n</code> consisting only of the characters <code>'A'</code> and <code>'B'</code>.</p>
|
|
|
|
<p>You are also given a 2D integer array <code>queries</code> of length <code>q</code>, where each <code>queries[i]</code> is one of the following:</p>
|
|
|
|
<ul>
|
|
<li><code>[1, j]</code>: <strong>Flip</strong> the character at index <code>j</code> of <code>s</code> i.e. <code>'A'</code> changes to <code>'B'</code> (and vice versa). This operation mutates <code>s</code> and affects subsequent queries.</li>
|
|
<li><code>[2, l, r]</code>: <strong>Compute</strong> the <strong>minimum</strong> number of character deletions required to make the <strong>substring</strong> <code>s[l..r]</code> <strong>alternating</strong>. This operation does not modify <code>s</code>; the length of <code>s</code> remains <code>n</code>.</li>
|
|
</ul>
|
|
|
|
<p>A <strong><span data-keyword="substring-nonempty">substring</span></strong> is <strong>alternating</strong> if no two <strong>adjacent</strong> characters are <strong>equal</strong>. A substring of length 1 is always alternating.</p>
|
|
|
|
<p>Return an integer array <code>answer</code>, where <code>answer[i]</code> is the result of the <code>i<sup>th</sup></code> query of type <code>[2, l, r]</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "ABA", queries = [[2,1,2],[1,1],[2,0,2]]</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">[0,2]</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 1, 2]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">[1, 1]</td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">Flip <code>s[1]</code> from <code>'B'</code> to <code>'A'</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"AAA"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"AAA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Delete any two <code>'A'</code>s to get <code>"A"</code></td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Thus, the answer is <code>[0, 2]</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "ABB", queries = [[2,0,2],[1,2],[2,0,2]]</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">[1,0]</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Delete one <code>'B'</code> to get <code>"AB"</code></td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">[1, 2]</td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABB"</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">Flip <code>s[2]</code> from <code>'B'</code> to <code>'A'</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 0, 2]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"ABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Thus, the answer is <code>[1, 0]</code>.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">s = "BABA", queries = [[2,0,3],[1,1],[2,1,3]]</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">[0,1]</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>i</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>queries[i]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>j</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>l</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>r</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong><code>s</code> before query</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><code><strong>s[l..r]</strong></code></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Result</strong></th>
|
|
<th align="center" style="border: 1px solid black;"><strong>Answer</strong></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 0, 3]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
<td align="center" style="border: 1px solid black;">3</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Already alternating</td>
|
|
<td align="center" style="border: 1px solid black;">0</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">[1, 1]</td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BABA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">Flip <code>s[1]</code> from <code>'A'</code> to <code>'B'</code></td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;">[2, 1, 3]</td>
|
|
<td align="center" style="border: 1px solid black;">-</td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
<td align="center" style="border: 1px solid black;">3</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BBBA"</code></td>
|
|
<td align="center" style="border: 1px solid black;"><code>"BBA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">Delete one <code>'B'</code> to get <code>"BA"</code></td>
|
|
<td align="center" style="border: 1px solid black;">1</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Thus, the answer is <code>[0, 1]</code>.</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[i]</code> is either <code>'A'</code> or <code>'B'</code>.</li>
|
|
<li><code>1 <= q == queries.length <= 10<sup>5</sup></code></li>
|
|
<li><code>queries[i].length == 2</code> or <code>3</code>
|
|
<ul>
|
|
<li><code>queries[i] == [1, j]</code> or,</li>
|
|
<li><code>queries[i] == [2, l, r]</code></li>
|
|
<li><code>0 <= j <= n - 1</code></li>
|
|
<li><code>0 <= l <= r <= n - 1</code></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|