implementation by Hash in C++ beats 64%


  • 0
    H

    class Solution {
    public:
    string getHint(string secret, string guess) {
    int bulls = 0;
    int cows = 0;
    int secretStr[10] = {0};
    int guessStr[10] = {0};
    int n = secret.size();
    for(int i = 0; i < n; i++)
    {
    if(secret[i] == guess[i])
    { bulls++;}
    else
    {
    secretStr[secret[i] - '0']++;
    guessStr[guess[i] - '0']++;
    }
    }
    for(int i = 0; i < 10; i++)
    {
    cows += min(secretStr[i], guessStr[i]);
    }

        return to_string(bulls) + 'A' + to_string(cows) + 'B';
    }
    

    };


Log in to reply
 

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