mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-26 18:20:27 +08:00
66 lines
3.0 KiB
HTML
66 lines
3.0 KiB
HTML
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>'A'</code> or by <code>'B'</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p>
|
|
|
|
<p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p>
|
|
|
|
<ul>
|
|
<li>Alice is only allowed to remove a piece colored <code>'A'</code> if <strong>both its neighbors</strong> are also colored <code>'A'</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>'B'</code>.</li>
|
|
<li>Bob is only allowed to remove a piece colored <code>'B'</code> if <strong>both its neighbors</strong> are also colored <code>'B'</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>'A'</code>.</li>
|
|
<li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li>
|
|
<li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li>
|
|
</ul>
|
|
|
|
<p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> colors = "AAABABB"
|
|
<strong>Output:</strong> true
|
|
<strong>Explanation:</strong>
|
|
A<u>A</u>ABABB -> AABABB
|
|
Alice moves first.
|
|
She removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.
|
|
|
|
Now it's Bob's turn.
|
|
Bob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.
|
|
Thus, Alice wins, so return true.
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> colors = "AA"
|
|
<strong>Output:</strong> false
|
|
<strong>Explanation:</strong>
|
|
Alice has her turn first.
|
|
There are only two 'A's and both are on the edge of the line, so she cannot move on her turn.
|
|
Thus, Bob wins, so return false.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> colors = "ABBBBBBBAAA"
|
|
<strong>Output:</strong> false
|
|
<strong>Explanation:</strong>
|
|
ABBBBBBBA<u>A</u>A -> ABBBBBBBAA
|
|
Alice moves first.
|
|
Her only option is to remove the second to last 'A' from the right.
|
|
|
|
ABBBB<u>B</u>BBAA -> ABBBBBBAA
|
|
Next is Bob's turn.
|
|
He has many options for which 'B' piece to remove. He can pick any.
|
|
|
|
On Alice's second turn, she has no more pieces that she can remove.
|
|
Thus, Bob wins, so return false.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= colors.length <= 10<sup>5</sup></code></li>
|
|
<li><code>colors</code> consists of only the letters <code>'A'</code> and <code>'B'</code></li>
|
|
</ul>
|