-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path299.java
32 lines (31 loc) · 938 Bytes
/
299.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.HashMap;
import java.util.Map;
class Solution {
public String getHint(String secret, String guess) {
Map<Character,Integer> map = new HashMap<>();
int bulls = 0;
String guessAfterBulls = "";
for (int i = 0; i < secret.length(); i++) {
char s = secret.charAt(i);
char g = guess.charAt(i);
if (s == g) {
bulls++;
} else {
guessAfterBulls += g;
if (map.containsKey(s)) {
map.put(s, map.get(s)+1);
} else {
map.put(s, 1);
}
}
}
int cows = 0;
for (char c: guessAfterBulls.toCharArray()) {
if (map.containsKey(c) && map.get(c) > 0) {
cows++;
map.put(c, map.get(c) - 1);
}
}
return bulls + "A" + cows + "B";
}
}