返回介绍

solution / 2000-2099 / 2038.Remove Colored Pieces if Both Neighbors are the Same Color / README_EN

发布于 2024-06-17 01:03:11 字数 6198 浏览 0 评论 0 收藏 0

2038. Remove Colored Pieces if Both Neighbors are the Same Color

中文文档

Description

There are n pieces arranged in a line, and each piece is colored either by 'A' or by 'B'. You are given a string colors of length n where colors[i] is the color of the ith piece.

Alice and Bob are playing a game where they take alternating turns removing pieces from the line. In this game, Alice moves first.

  • Alice is only allowed to remove a piece colored 'A' if both its neighbors are also colored 'A'. She is not allowed to remove pieces that are colored 'B'.
  • Bob is only allowed to remove a piece colored 'B' if both its neighbors are also colored 'B'. He is not allowed to remove pieces that are colored 'A'.
  • Alice and Bob cannot remove pieces from the edge of the line.
  • If a player cannot make a move on their turn, that player loses and the other player wins.

Assuming Alice and Bob play optimally, return true_ if Alice wins, or return _false_ if Bob wins_.

 

Example 1:

Input: colors = "AAABABB"
Output: true
Explanation:
AAABABB -> 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.

Example 2:

Input: colors = "AA"
Output: false
Explanation:
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.

Example 3:

Input: colors = "ABBBBBBBAAA"
Output: false
Explanation:
ABBBBBBBAAA -> ABBBBBBBAA
Alice moves first.
Her only option is to remove the second to last 'A' from the right.

ABBBBBBBAA -> 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.

 

Constraints:

  • 1 <= colors.length <= 105
  • colors consists of only the letters 'A' and 'B'

Solutions

Solution 1: Counting

We count the number of times that the string colors contains three consecutive 'A's or three consecutive 'B's, denoted as $a$ and $b$, respectively.

Finally, we check whether $a$ is greater than $b$. If it is, we return true. Otherwise, we return false.

The time complexity is $O(n)$, where $n$ is the length of the string colors. The space complexity is $O(1)$.

class Solution:
  def winnerOfGame(self, colors: str) -> bool:
    a = b = 0
    for c, v in groupby(colors):
      m = len(list(v)) - 2
      if m > 0 and c == 'A':
        a += m
      elif m > 0 and c == 'B':
        b += m
    return a > b
class Solution {
  public boolean winnerOfGame(String colors) {
    int n = colors.length();
    int a = 0, b = 0;
    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && colors.charAt(j) == colors.charAt(i)) {
        ++j;
      }
      int m = j - i - 2;
      if (m > 0) {
        if (colors.charAt(i) == 'A') {
          a += m;
        } else {
          b += m;
        }
      }
    }
    return a > b;
  }
}
class Solution {
public:
  bool winnerOfGame(string colors) {
    int n = colors.size();
    int a = 0, b = 0;
    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && colors[j] == colors[i]) {
        ++j;
      }
      int m = j - i - 2;
      if (m > 0) {
        if (colors[i] == 'A') {
          a += m;
        } else {
          b += m;
        }
      }
    }
    return a > b;
  }
};
func winnerOfGame(colors string) bool {
  n := len(colors)
  a, b := 0, 0
  for i, j := 0, 0; i < n; i = j {
    for j < n && colors[j] == colors[i] {
      j++
    }
    m := j - i - 2
    if m > 0 {
      if colors[i] == 'A' {
        a += m
      } else {
        b += m
      }
    }
  }
  return a > b
}
function winnerOfGame(colors: string): boolean {
  const n = colors.length;
  let [a, b] = [0, 0];
  for (let i = 0, j = 0; i < n; i = j) {
    while (j < n && colors[j] === colors[i]) {
      ++j;
    }
    const m = j - i - 2;
    if (m > 0) {
      if (colors[i] === 'A') {
        a += m;
      } else {
        b += m;
      }
    }
  }
  return a > b;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文