C# Solution One Pass


  • 0
    K
        public string GetHint(string secret, string guess)
        {
            int bulls = 0;
            int cows = 0;
            var dict = new Dictionary<char,int>();
            for(int i = 0; i < secret.Length; ++i){
                if (guess[i] == secret[i]){
                    bulls++;
                }
                else{
                    if (!dict.ContainsKey(secret[i])){
                        dict[secret[i]] = 0;
                    }
                    if (!dict.ContainsKey(guess[i])){
                        dict[guess[i]] = 0;
                    }
                    if (dict[secret[i]]++ < 0)
                        cows++;
                    if (dict[guess[i]]-- > 0)
                        cows++;
                }
            }
            
            
            return bulls + "A" + cows + "B";
        }
    }

Log in to reply
 

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