Easy c++ solution


  • 0
    K
    class Solution {
    public:
        string getHint(string secret, string guess) {
            string res;
            vector<int> map_secret(10, 0);
            vector<int> map_guess(10, 0);
            int bull = 0, cow = 0;
            for (int i = 0; i<secret.size(); ++i) {
                if (secret[i] == guess[i]) ++bull;
                else {
                    ++map_secret[secret[i]-'0'];
                    ++map_guess[guess[i]-'0'];
                }
            }
            for (int i = 0; i<10; ++i) {
                cow += min(map_secret[i], map_guess[i]);
            }
            return to_string(bull) + "A" + to_string(cow) + "B";
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.