返回介绍

lcci / 16.15.Master Mind / README_EN

发布于 2024-06-17 01:04:42 字数 4892 浏览 0 评论 0 收藏 0

16.15. Master Mind

中文文档

Description

The Game of Master Mind is played as follows:

The computer has four slots, and each slot will contain a ball that is red (R). yellow (Y). green (G) or blue (B). For example, the computer might have RGGB (Slot #1 is red, Slots #2 and #3 are green, Slot #4 is blue).

You, the user, are trying to guess the solution. You might, for example, guess YRGB.

When you guess the correct color for the correct slot, you get a "hit:' If you guess a color that exists but is in the wrong slot, you get a "pseudo-hit:' Note that a slot that is a hit can never count as a pseudo-hit.

For example, if the actual solution is RGBY and you guess GGRR, you have one hit and one pseudo-hit. Write a method that, given a guess and a solution, returns the number of hits and pseudo-hits.

Given a sequence of colors solution, and a guess, write a method that return the number of hits and pseudo-hit answer, where answer[0] is the number of hits and answer[1] is the number of pseudo-hit.

Example:


Input:  solution="RGBY",guess="GGRR"

Output:  [1,1]

Explanation:  hit once, pseudo-hit once.

Note:

  • len(solution) = len(guess) = 4
  • There are only "R","G","B","Y" in solution and guess.

Solutions

Solution 1: Hash Table

We simultaneously traverse both strings, count the number of corresponding characters that are the same, and accumulate them in $x$. Then we record the characters and their frequencies in both strings in hash tables $cnt1$ and $cnt2$, respectively.

Next, we traverse both hash tables, count the number of common characters, and accumulate them in $y$. The answer is then $[x, y - x]$.

The time complexity is $O(C)$, and the space complexity is $O(C)$. Here, $C=4$ for this problem.

class Solution:
  def masterMind(self, solution: str, guess: str) -> List[int]:
    x = sum(a == b for a, b in zip(solution, guess))
    y = sum((Counter(solution) & Counter(guess)).values())
    return [x, y - x]
class Solution {
  public int[] masterMind(String solution, String guess) {
    int x = 0, y = 0;
    Map<Character, Integer> cnt1 = new HashMap<>();
    Map<Character, Integer> cnt2 = new HashMap<>();
    for (int i = 0; i < 4; ++i) {
      char a = solution.charAt(i), b = guess.charAt(i);
      x += a == b ? 1 : 0;
      cnt1.merge(a, 1, Integer::sum);
      cnt2.merge(b, 1, Integer::sum);
    }
    for (char c : "RYGB".toCharArray()) {
      y += Math.min(cnt1.getOrDefault(c, 0), cnt2.getOrDefault(c, 0));
    }
    return new int[] {x, y - x};
  }
}
class Solution {
public:
  vector<int> masterMind(string solution, string guess) {
    int x = 0, y = 0;
    unordered_map<char, int> cnt1;
    unordered_map<char, int> cnt2;
    for (int i = 0; i < 4; ++i) {
      x += solution[i] == guess[i];
      cnt1[solution[i]]++;
      cnt2[guess[i]]++;
    }
    for (char c : "RYGB") y += min(cnt1[c], cnt2[c]);
    return vector<int>{x, y - x};
  }
};
func masterMind(solution string, guess string) []int {
  var x, y int
  cnt1 := map[byte]int{}
  cnt2 := map[byte]int{}
  for i := range solution {
    a, b := solution[i], guess[i]
    if a == b {
      x++
    }
    cnt1[a]++
    cnt2[b]++
  }
  for _, c := range []byte("RYGB") {
    y += min(cnt1[c], cnt2[c])
  }
  return []int{x, y - x}
}
/**
 * @param {string} solution
 * @param {string} guess
 * @return {number[]}
 */
var masterMind = function (solution, guess) {
  let counts1 = { R: 0, G: 0, B: 0, Y: 0 };
  let counts2 = { R: 0, G: 0, B: 0, Y: 0 };
  let res1 = 0;
  for (let i = 0; i < solution.length; i++) {
    let s1 = solution.charAt(i),
      s2 = guess.charAt(i);
    if (s1 == s2) {
      res1++;
    } else {
      counts1[s1] += 1;
      counts2[s2] += 1;
    }
  }
  let res2 = ['R', 'G', 'B', 'Y'].reduce((a, c) => a + Math.min(counts1[c], counts2[c]), 0);
  return [res1, res2];
};

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

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

发布评论

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