-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullsAndCows.java
More file actions
37 lines (31 loc) · 941 Bytes
/
Copy pathBullsAndCows.java
File metadata and controls
37 lines (31 loc) · 941 Bytes
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
33
34
35
36
37
/**
* Created by Baiye on 2016/12/13.
*/
/**
* LeetCode.299.Bulls and Cows
*/
public class BullsAndCows {
public String getHint(String secret, String guess) {
if (secret == null || guess == null || secret.length() != guess.length()) {
return "";
}
int countA = 0;
int countB = 0;
int[] count = new int[10];
for (int i = 0; i < secret.length(); i++) {
if (secret.charAt(i) == guess.charAt(i)) {
countA++;
} else {
count[secret.charAt(i) - '0']++;
if (count[secret.charAt(i) - '0'] <= 0) {
countB++;
}
count[guess.charAt(i)- '0']--;
if (count[guess.charAt(i)- '0'] >= 0) {
countB++;
}
}
}
return String.valueOf(countA) + "A" + String.valueOf(countB) + "B";
}
}