C
C#15mo ago
morry329#

❔ Getting a wrong result on LeetCode although the IDE shows the correct output

So I believe I still make some mistake in setting cows' Count for this puzzle https://leetcode.com/problems/bulls-and-cows/submissions/ For the test case string secret = "1807", guess = "7810";I get the wrong output 1A0B on the website while getting the correct one (1A3B) on Rider (IDE). Can anyone please point me in the right direction or is LC malfunctioning? public class Solution { public string GetHint(string secret, string guess) { var dic = new Dictionary<char, int>(); int bulls = 0, cows = 0; for (int i = 0; i < secret.Length; i++) { if (!dic.ContainsKey(secret[i])) { dic.TryAdd(secret[i], 0); } } for (int i = 0; i < secret.Length; i++) { if (secret[i] == guess[i]) { bulls++; dic[secret[i]]++; } } foreach (var k in dic) { /*if (!dic.TryGetValue(k.Key, out int val)) { continue; }*/ cows += Math.Min(k.Value, dic.GetValueOrDefault(guess[k.Key],0)); } Console.WriteLine(bulls + "A" + cows + "B"); return $"{bulls}A{cows}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.
7 Replies
Moods
Moods15mo ago
imma be real i ran your code on the lc thingy and it threw an exception sorry i was solving it okay i dont think your approach was completely wrong the easy part you did second, but can you explain what the first and third loops are meant to do?
morry329#
morry329#OP15mo ago
So the first loop prepares the dictionary for the iterations at the second and third loops (if the dic has the corresponding char in the secret string, it tries to add it into the dic - but without modifying the dictionary). The third loop tries to get the minimum from the secret key and the guess key each, so the dictionary will not count the duplicate cows I think I still have an error with handling the cows in the third loop, as the count of bulls is fine
Moods
Moods15mo ago
Hmm, I’m not following your logic with your approach Can i just tell you what I did or would that be cheating
morry329#
morry329#OP15mo ago
My logic is wrong then Yes please tell me what you did - I am totally swamped with this puzzle haha
Moods
Moods15mo ago
public string GetHint(string secret, string guess) {
int bulls = 0, cows = 0;

var foo = new Dictionary<int, int>();
var hashSet = new HashSet<int>();

for(int a = 0; a < guess.Length; a++)
if(guess[a] == secret[a])
{
bulls++;
hashSet.Add(a);
}
else{
if(foo.ContainsKey(secret[a])) foo[secret[a]]++;
else foo.Add(secret[a], 1);
}

for(int a = 0; a < guess.Length; a++)
if(!hashSet.Contains(a))
if(foo.ContainsKey(guess[a]))
{
cows++; foo[guess[a]]--;
if(foo[guess[a]] == 0) foo.Remove(guess[a]);
}

return $"{bulls}A{cows}B";
}
public string GetHint(string secret, string guess) {
int bulls = 0, cows = 0;

var foo = new Dictionary<int, int>();
var hashSet = new HashSet<int>();

for(int a = 0; a < guess.Length; a++)
if(guess[a] == secret[a])
{
bulls++;
hashSet.Add(a);
}
else{
if(foo.ContainsKey(secret[a])) foo[secret[a]]++;
else foo.Add(secret[a], 1);
}

for(int a = 0; a < guess.Length; a++)
if(!hashSet.Contains(a))
if(foo.ContainsKey(guess[a]))
{
cows++; foo[guess[a]]--;
if(foo[guess[a]] == 0) foo.Remove(guess[a]);
}

return $"{bulls}A{cows}B";
}
getting the bulls was the easy part so i counted that first. I also put the indices the bulls occured at so that i can ignore them in the second loop. also in the first loop i added the secret characters that werent bull into a dictionary with the amount of times they appeared(i could have converted the chars to the actual value they were but i figured it didnt matter so i implicitly converted from char to int (which should have stored the ASCII code)) then in the second loop i simply iterated through guess and if it was in an indice that wasnt a bull i would check if that character had appeared in secret(via the dictionary) and if it has id reduce the count of the char in the dictionary and increase the count of cows there's a way to do it in one pass though(checked other people's solutions afterwards) but ehhh can't be arsed
morry329#
morry329#OP15mo ago
Thank you so much for this - It did not occur to me that HashSet can be applied here °° Yeah, one pass solution would be complicated (and my brain cannot take that complex code lol)
Accord
Accord15mo ago
Looks like nothing has happened here. 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