© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
morry329#

❔ ✅ My code is pretty close to the right solution, but one test case still fails

So I have this solution for LeetCode's bulls and cows puzzle https://leetcode.com/problems/bulls-and-cows/
One test case still fails as per screenshot. The code for checking cows' count is not correct obviously, but I have no idea at all how to fix it. Could anyone kindly point me in the right direction?

public class Solution {
    public string GetHint(string secret, string guess) {
            int[] cows = new int[10];
            int[] bulls = new int[10];
        
            int bullCount = 0;
        
            //bulls
            for(int i = 0; i < secret.Length; i++){
                if(secret[i] == guess[i]){
                    bullCount++;
                
                } else {
                    bulls[secret[i]-'0']++;
                    cows[guess[i]-'0']++;
                }
            
            }
        
            int cowCount = 0;
        
            //cows 
            for(int j = 0; j < guess.Length; j++){
                if(secret[j] != guess[j] && cows[guess[j]-'0'] > 0)
                {
                    cowCount++;
                    cows[guess[j]-'0']--;
                }
                
                
            }
            
            Console.WriteLine(cowCount);
            Console.WriteLine($"{bullCount} A {cowCount} B");
            return $"{bullCount}A{cowCount}B";
        }
}
public class Solution {
    public string GetHint(string secret, string guess) {
            int[] cows = new int[10];
            int[] bulls = new int[10];
        
            int bullCount = 0;
        
            //bulls
            for(int i = 0; i < secret.Length; i++){
                if(secret[i] == guess[i]){
                    bullCount++;
                
                } else {
                    bulls[secret[i]-'0']++;
                    cows[guess[i]-'0']++;
                }
            
            }
        
            int cowCount = 0;
        
            //cows 
            for(int j = 0; j < guess.Length; j++){
                if(secret[j] != guess[j] && cows[guess[j]-'0'] > 0)
                {
                    cowCount++;
                    cows[guess[j]-'0']--;
                }
                
                
            }
            
            Console.WriteLine(cowCount);
            Console.WriteLine($"{bullCount} A {cowCount} B");
            return $"{bullCount}A{cowCount}B";
        }
}
Bildschirmfoto_2023-08-17_um_17.09.31.png
LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
LeetCode - The World's Leading Online Programming Learning Platform
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Why is my code still wrong?
C#CC# / help
3y ago
❔ Is the functionality of my code in the right place
C#CC# / help
3y ago
✅ Case 3 of my code.
C#CC# / help
3y ago
✅ why does the test case fail?
C#CC# / help
4y ago