面试题 16.15. 珠玑妙算

面试题 16.15. 珠玑妙算
珠玑妙算游戏(the game of master mind)的玩法如下。

计算机有4个槽,每个槽放一个球,颜色可能是红色(R)、黄色(Y)、绿色(G)或蓝色(B)。例如,计算机可能有RGGB 4种(槽1为红色,槽2、3为绿色,槽4为蓝色)。作为用户,你试图猜出颜色组合。打个比方,你可能会猜YRGB。要是猜对某个槽的颜色,则算一次“猜中”;要是只猜对颜色但槽位猜错了,则算一次“伪猜中”。注意,“猜中”不能算入“伪猜中”。

给定一种颜色组合solution和一个猜测guess,编写一个方法,返回猜中和伪猜中的次数answer,其中answer[0]为猜中的次数,answer[1]为伪猜中的次数。

示例:

输入: solution="RGBY",guess="GGRR"
输出: [1,1]
解释: 猜中1次,伪猜中1次。
class Solution {
public:
    vector<int> masterMind(string solution, string guess) {
        // vector<int> res(2, 0);
        int len = solution.size();
        int count = 0;
        unordered_map<char, int> s_ump, g_ump;
        for(auto s_it = solution.begin(), g_it = guess.begin(); s_it != solution.end() && g_it != guess.end(); ++s_it, ++g_it){
            if(*s_it == *g_it){
                ++count;
            } else {
                ++s_ump[*s_it];
                ++g_ump[*g_it];
            }
        }
        int num = 0;
        for(char c : "RGBY"){
            num += min(s_ump[c], g_ump[c]);
        }
        return vector<int>{count, num};
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43599304/article/details/121284974