{"id":8620,"date":"2021-10-18T17:55:36","date_gmt":"2021-10-19T00:55:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8620"},"modified":"2021-10-18T21:50:17","modified_gmt":"2021-10-19T04:50:17","slug":"leetcode-2038-remove-colored-pieces-if-both-neighbors-are-the-same-color","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2038-remove-colored-pieces-if-both-neighbors-are-the-same-color\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2038. Remove Colored Pieces if Both Neighbors are the Same Color"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;pieces arranged in a line, and each piece is colored either by&nbsp;<code>'A'<\/code>&nbsp;or by&nbsp;<code>'B'<\/code>. You are given a string&nbsp;<code>colors<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>colors[i]<\/code>&nbsp;is the color of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;piece.<\/p>\n\n\n\n<p>Alice and Bob are playing a game where they take&nbsp;<strong>alternating turns<\/strong>&nbsp;removing pieces from the line. In this game, Alice moves<strong>&nbsp;first<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Alice is only allowed to remove a piece colored&nbsp;<code>'A'<\/code>&nbsp;if&nbsp;<strong>both its neighbors<\/strong>&nbsp;are also colored&nbsp;<code>'A'<\/code>. She is&nbsp;<strong>not allowed<\/strong>&nbsp;to remove pieces that are colored&nbsp;<code>'B'<\/code>.<\/li><li>Bob is only allowed to remove a piece colored&nbsp;<code>'B'<\/code>&nbsp;if&nbsp;<strong>both its neighbors<\/strong>&nbsp;are also colored&nbsp;<code>'B'<\/code>. He is&nbsp;<strong>not allowed<\/strong>&nbsp;to remove pieces that are colored&nbsp;<code>'A'<\/code>.<\/li><li>Alice and Bob&nbsp;<strong>cannot<\/strong>&nbsp;remove pieces from the edge of the line.<\/li><li>If a player cannot make a move on their turn, that player&nbsp;<strong>loses<\/strong>&nbsp;and the other player&nbsp;<strong>wins<\/strong>.<\/li><\/ul>\n\n\n\n<p>Assuming Alice and Bob play optimally, return&nbsp;<code>true<\/code><em>&nbsp;if Alice wins, or return&nbsp;<\/em><code>false<\/code><em>&nbsp;if Bob wins<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = \"AAABABB\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nAAABABB -&gt; AABABB\nAlice moves first.\nShe removes the second 'A' from the left since that is the only 'A' whose neighbors are both 'A'.\n\nNow it's Bob's turn.\nBob cannot make a move on his turn since there are no 'B's whose neighbors are both 'B'.\nThus, Alice wins, so return true.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = \"AA\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nAlice has her turn first.\nThere are only two 'A's and both are on the edge of the line, so she cannot move on her turn.\nThus, Bob wins, so return false.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> colors = \"ABBBBBBBAAA\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nABBBBBBBAAA -&gt; ABBBBBBBAA\nAlice moves first.\nHer only option is to remove the second to last 'A' from the right.\n\nABBBBBBBAA -&gt; ABBBBBBAA\nNext is Bob's turn.\nHe has many options for which 'B' piece to remove. He can pick any.\n\nOn Alice's second turn, she has no more pieces that she can remove.\nThus, Bob wins, so return false.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>colors<\/code>&nbsp;consists of only the letters&nbsp;<code>'A'<\/code>&nbsp;and&nbsp;<code>'B'<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>Count how many &#8216;AAA&#8217;s and &#8216;BBB&#8217;s. <\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool winnerOfGame(string colors) {\n    const int n = colors.size();\n    int count = 0;\n    for (int i = 1; i < n - 1; ++i)\n      if (colors[i - 1] == colors[i] &#038;&#038;\n          colors[i] == colors[i + 1])\n        count += (colors[i] == 'A' ? 1 : -1);    \n    return count > 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;pieces arranged in a line, and each piece is colored either by&nbsp;&#8216;A&#8217;&nbsp;or by&nbsp;&#8216;B&#8217;. You are given a string&nbsp;colors&nbsp;of length&nbsp;n&nbsp;where&nbsp;colors[i]&nbsp;is the color of the&nbsp;ith&nbsp;piece. Alice&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,8],"class_list":["post-8620","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-counting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8620"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8620\/revisions"}],"predecessor-version":[{"id":8622,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8620\/revisions\/8622"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}