C# Time O(n) Space O(1)


  • 0
    L
    public string GetHint(string secret, string guess) {
        int bull = 0, cow = 0;
        int[] setSecret = new int[10], listGuess = new int[10];
        for(int i = 0; i < secret.Length; i++)
            if(secret[i] == guess[i]) bull++;
            else{
                setSecret[secret[i] - '0']++;
                listGuess[guess[i] - '0']++;
            }
        for(int i = 0; i < 10; i++)
            while(listGuess[i]-- > 0 && setSecret[i]-- > 0)
                cow++;
        return bull + "A" + cow + "B";
    }

Log in to reply
 

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