My c solution, use hashtable


  • 0
    M

    use hashtable

     char* getHint(char* secret, char* guess) {
            int bulls = 0; 
            int cows = 0;
            int hash[10] = {0};
            char result[10];
            
            int len = strlen(secret);
            for (int i = 0; i < len; i++){
                hash[secret[i]-'0']++;
                if (secret[i] == guess[i])
                    bulls ++;
            }
            
            for (int i = 0; i < len; i++){
                if (hash[guess[i]-'0']){cows++; hash[guess[i]-'0']--;}
            }
            
            sprintf(result, "%dA%dB", bulls, cows-bulls);
            return result;
        }

Log in to reply
 

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