My One pass C++ solution with 4ms


  • 0
    L
     string getHint(string secret, string guess) {
        int record[10] = {0},bulls = 0, cows = 0;
        int len = min(secret.size(),guess.size());
        for(int i = 0; i != len; ++i)
        {
            if(secret[i] == guess[i])
                ++bulls;
            else
            {
                int & decrease = record[secret[i] - '0'];
                int & increase = record[guess[i] - '0'];
                if(decrease > 0) ++cows;
                if(increase < 0) ++cows;
                --decrease;
                ++increase;
            }
        }
        return to_string(bulls)+ "A" + to_string(cows) + "B";
    }

  • 0
    S

    hi,,I can't understand the increase decrease part.. Can you please help me out??


Log in to reply
 

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