-
Notifications
You must be signed in to change notification settings - Fork 5
/
BullsAndCows.java
64 lines (61 loc) · 1.76 KB
/
BullsAndCows.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*https://leetcode.com/problems/bulls-and-cows/*/
class Solution {
public String getHint(String secret, String guess) {
char[] s = secret.toCharArray(), g = guess.toCharArray();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int n = s.length, i, bullCount = 0, cowCount = 0;
for (i = 0; i < n; ++i)
{
if (s[i] == g[i])
{
++bullCount;
s[i] = g[i] = '\0';
}
else
{
map.put(s[i],map.containsKey(s[i]) ? (Integer)map.get(s[i])+1 : 1);
}
}
for (i = 0; i < n; ++i)
{
if (g[i] != '\0' && map.containsKey(g[i]) && map.get(g[i]) > 0)
{
++cowCount;
map.put(g[i],(Integer)map.get(g[i])-1);
}
}
StringBuffer result = new StringBuffer("");
result.append(bullCount);
result.append("A");
result.append(cowCount);
result.append("B");
return result.toString();
}
}
class Solution {
public String getHint(String secret, String guess)
{
int[] h = new int[10];
int bulls = 0, cows = 0;
int n = guess.length();
for(int i = 0; i < n; i++)
{
char s = secret.charAt(i);
char g = guess.charAt(i);
if(s == g) bulls++;
else
{
if(h[s - '0'] < 0) cows++;
if(h[g - '0'] > 0) cows++;
h[s-'0']++;
h[g-'0']--;
}
}
StringBuilder sb = new StringBuilder();
sb.append(bulls);
sb.append("A");
sb.append(cows);
sb.append("B");
return sb.toString();
}
}