java.lang.Object
g2001_2100.s2038_remove_colored_pieces_if_both_neighbors_are_the_same_color.Solution

public class Solution extends Object
2038 - Remove Colored Pieces if Both Neighbors are the Same Color.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> colors = &ldquo;AAABABB&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong></p> <p>AAABABB -> AABABB</p> <p>Alice moves first.</p> <p>She removes the second &lsquo;A&rsquo; from the left since that is the only &lsquo;A&rsquo; whose neighbors are both &lsquo;A&rsquo;.</p> <p>Now it&rsquo;s Bob&rsquo;s turn.</p> <p>Bob cannot make a move on his turn since there are no &rsquo;B&rsquo;s whose neighbors are both &lsquo;B&rsquo;.</p> <p>Thus, Alice wins, so return true.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> colors = &ldquo;AA&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong></p> <p>Alice has her turn first.</p> <p>There are only two &rsquo;A&rsquo;s and both are on the edge of the line, so she cannot move on her turn.</p> <p>Thus, Bob wins, so return false.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> colors = &ldquo;ABBBBBBBAAA&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong></p> <p>ABBBBBBBAAA -> ABBBBBBBAA</p> <p>Alice moves first.</p> <p>Her only option is to remove the second to last &lsquo;A&rsquo; from the right.</p> <p>ABBBBBBBAA -> ABBBBBBAA</p> <p>Next is Bob&rsquo;s turn.</p> <p>He has many options for which &lsquo;B&rsquo; piece to remove.</p> <p>He can pick any. On Alice&rsquo;s second turn, she has no more pieces that she can remove.</p> <p>Thus, Bob wins, so return false.</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • winnerOfGame

      public boolean winnerOfGame(String colors)