C
C#11mo ago
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"; } }
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.
3 Replies
SinFluxx
SinFluxx11mo ago
You'll need to make sure you're not counting a character from the guess string more than once when that character appears multiple times in the secret string
morry329#
morry329#11mo ago
👍 Ok I have one more question @sinfluxx I tried to remove the duplicate cow counts, but one of my code lines seems to be wrong. Maybe do you have any feedback on this? 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 wrong = 0; //cows for(int j = 0; j < guess.Length; j++){ if(secret[j] != guess[j] && cows[guess[j]-'0'] > 0) { wrong++; cows[guess[j]-'0']--; } } int cowCount = secret.Length-bullCount-wrong; //this line does not seem to work Console.WriteLine(cowCount); Console.WriteLine($"{bullCount} A {cowCount} B"); return $"{bullCount}A{cowCount}B"; }
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Has anyone worked on PUBNUB c# sdk?Need inputs on publishing and subscribing. Please help❔ Reordering of columns: (DevExpress/XAML)How should I go about switching the order of these columns? I tried the following but did not work: ❔ When using AbstractValidator, is there any way that i can override the http status code?Im trying to return a 422 HTTP Status code when the error occurs, but by default it returns a 400 Ba❔ Best way to structure MVVM WPF App for multiples popupsHi ! I have a (nooby) WPF question. So I'm using MVVM paterns Here is my folder structure: ``` /MVVM❔ anyone uses/used/run splashkit on visual studio from iOS/MacBooks?Or any recommendation on tutorials ? Most tutorials I’ve seen were on windows and lowkey getting con❔ Whats the fastest way to write this data to a file? (C# Console App.)I need to write an int[][] to disk. Each int[] has 2 values, and the full int[][] has 100mil entries❔ Thin/Thick UI DesignHi! I've been a longtime c#.net programmer at a corporate office. I've been wanting to do some side Visual Studio project reference not updating with new codeBasically I have 2 projects in separate solutions, API and Data (the projects) API usually reference✅ HTTP Error 500.30 - ASP.NET Core app failed to startHello! I'm getting this error after deploying a net core app to IIS 10 (Windows 10 pc). Publish se✅ what is a .zero resource file?im looking at some code for an encoder/decoder and it uses a resource file called Aura.zero but its