Created
October 9, 2020 03:10
-
-
Save riyafa/873b61a1c83fe7c6eea68fb6b16122fb to your computer and use it in GitHub Desktop.
Solution for LeetCode Bulls and Cows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BullsAndCows { | |
public String getHint(String secret, String guess) { | |
int n = secret.length(); | |
int[] map = new int[10]; | |
int a = 0; | |
int b = 0; | |
for (int i = 0; i < n; i++) { | |
int s = secret.charAt(i) - '0'; | |
int g = guess.charAt(i) - '0'; | |
if (s == g) { | |
a++; | |
} else { | |
if (map[s] < 0) { | |
b++; | |
} | |
if (map[g] > 0) { | |
b++; | |
} | |
map[s]++; | |
map[g]--; | |
} | |
} | |
return a + "A" + b + "B"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment