c++ use map


  • 0
    S

    string getHint(string secret, string guess) {
    map<char,int> m1,m2;
    for(int i=0;i<=9;++i){
    m1[char(int('0')+i)]=0;
    m2[char(int('0')+i)]=0;
    }
    int bull=0, cow=0;
    for(int i=0;i<secret.length();++i){
    if(secret[i]==guess[i]){
    bull++;
    }
    else{
    m1[secret[i]]++;
    m2[guess[i]]++;
    }
    }
    for(int i=0;i<=9;++i){
    cow+=min(m1[char(int('0')+i)],m2[char(int('0')+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.