My 4ms c++ O(n) solution


  • 0
    Y
        int n = secret.size();
        int t1[10] = {0},t2[10] = {0};
        int A=0,B=0;
        for(int i = 0;i < n;++i){
            if(secret[i] == guess[i]) A++;
            else{
                if(t2[secret[i] - 48] != 0){
                    B++;
                    t2[secret[i] - 48]--;
                }else{
                    t1[secret[i] - 48]++;
                }
                
                if(t1[guess[i] -48] != 0){
                    B++;
                    t1[guess[i] -48]--;
                }else{
                    t2[guess[i] -48]++;
                }
            }
        }
        string s;
        s += to_string(A) + "A" + to_string(B) + "B";
        return s;

Log in to reply
 

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