-
Notifications
You must be signed in to change notification settings - Fork 0
/
299_2.java
30 lines (24 loc) · 1.02 KB
/
299_2.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
public String getHint(String secret, String guess) {
//table records the appearance of a digit
//digit from secret will increase the counter
//digit from guess will decrease the counter
int[] count = new int[10];
int counterA = 0, counterB = 0;
for(int i = 0; i < secret.length(); i++){
int a = secret.charAt(i) - '0', b = guess.charAt(i) - '0';
if( a == b){
//accurate match (same digit with same position)
counterA ++;
}else{
//if prev part of guess has curr digit in secret
//then we found a pair that has same digit with different position
if(count[a] < 0) counterB ++;
//if prev part of secret has curr digit in guess
//then we found a pair that has same digit with different position
if(count[b] > 0) counterB ++;
count[a] ++;
count[b] --;
}
}
return counterA + "A" + counterB + "B";
}