Easy to understand C++ 3m solution, using int[10] as hashtable.


  • 0
    J
     string getHint(string secret, string guess) {
            
            int hash[10] = {0};
    
            int A = 0;
            for(int i = 0 ; i < secret.size() ; i++){
                if(secret[i] == guess[i]) A++;
                else hash[guess[i]-'0']++;
                
            }
            
            int B = 0;        
            for(int i = 0 ; i < secret.size() ; i++){
                if(secret[i] != guess[i] && hash[secret[i] - '0']){
                    B++;
                    hash[secret[i]-'0']--;
                }
            }
            
            
            return to_string(A)+"A"+to_string(B)+"B";
        }

Log in to reply
 

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