@@ -63,7 +63,6 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul
63
63
<li><code>secret</code> and <code>guess</code> consist of digits only.</li>
64
64
</ul >
65
65
66
-
67
66
## Solutions
68
67
69
68
<!-- tabs:start -->
@@ -73,45 +72,98 @@ Note that only one of the two unmatched 1s is counted as a cow since the non-bul
73
72
``` python
74
73
class Solution :
75
74
def getHint (self , secret : str , guess : str ) -> str :
76
- a_cnt = b_cnt = 0
77
- nums1 = dict ()
78
- nums2 = dict ()
75
+ x = y = 0
76
+ cnt1 = [ 0 ] * 10
77
+ cnt2 = [ 0 ] * 10
79
78
for i in range (len (secret)):
80
79
if secret[i] == guess[i]:
81
- a_cnt += 1
80
+ x += 1
82
81
else :
83
- nums1[secret[i]] = nums1.get (secret[i], 0 ) + 1
84
- nums2[guess[i]] = nums2.get (guess[i], 0 ) + 1
85
- for i, v in nums1.items():
86
- if i in nums2 :
87
- b_cnt += min (v, nums2 [i])
88
- return f ' { a_cnt } A { b_cnt } B '
82
+ cnt1[ int (secret[i])] += 1
83
+ cnt2[ int (guess[i])] += 1
84
+
85
+ for i in range ( 10 ) :
86
+ y += min (cnt1[i], cnt2 [i])
87
+ return f ' { x } A { y } B '
89
88
```
90
89
91
90
### ** Java**
92
91
93
92
``` java
94
93
class Solution {
95
94
public String getHint (String secret , String guess ) {
96
- int aCnt = 0 , bCnt = 0 ;
97
- Map< Character , Integer > nums1 = new HashMap<> () ;
98
- Map< Character , Integer > nums2 = new HashMap<> () ;
95
+ int x = 0 , y = 0 ;
96
+ int [] cnt1 = new int [ 10 ] ;
97
+ int [] cnt2 = new int [ 10 ] ;
99
98
for (int i = 0 ; i < secret. length(); ++ i) {
100
- if (secret. charAt(i) == guess. charAt(i)) {
101
- ++ aCnt;
99
+ int a = secret. charAt(i) - ' 0' , b = guess. charAt(i) - ' 0' ;
100
+ if (a == b) {
101
+ ++ x;
102
102
} else {
103
- nums1 . put(secret . charAt(i), nums1 . getOrDefault(secret . charAt(i), 0 ) + 1 ) ;
104
- nums2 . put(guess . charAt(i), nums2 . getOrDefault(guess . charAt(i), 0 ) + 1 ) ;
103
+ ++ cnt1[a] ;
104
+ ++ cnt2[b] ;
105
105
}
106
106
}
107
+ for (int i = 0 ; i < 10 ; ++ i) {
108
+ y += Math . min(cnt1[i], cnt2[i]);
109
+ }
110
+ return String . format(" %dA%dB" , x, y);
111
+ }
112
+ }
113
+ ```
107
114
108
- for (Map . Entry<Character , Integer > entry : nums1. entrySet()) {
109
- if (nums2. containsKey(entry. getKey())) {
110
- bCnt += Math . min(entry. getValue(), nums2. get(entry. getKey()));
115
+ ### ** C++**
116
+
117
+ ``` cpp
118
+ class Solution {
119
+ public:
120
+ string getHint(string secret, string guess) {
121
+ int x = 0, y = 0;
122
+ vector<int > cnt1(10);
123
+ vector<int > cnt2(10);
124
+ for (int i = 0; i < secret.size(); ++i)
125
+ {
126
+ int a = secret[ i] - '0', b = guess[ i] - '0';
127
+ if (a == b) ++x;
128
+ else
129
+ {
130
+ ++cnt1[ a] ;
131
+ ++cnt2[ b] ;
111
132
}
112
133
}
113
- return String . format(" %dA%dB" , aCnt, bCnt);
134
+ for (int i = 0; i < 10; ++i) y += min(cnt1[ i] , cnt2[ i] );
135
+ return to_string(x) + "A" + to_string(y) + "B";
114
136
}
137
+ };
138
+ ```
139
+
140
+ ### **Go**
141
+
142
+ ```go
143
+ func getHint(secret string, guess string) string {
144
+ x, y := 0, 0
145
+ cnt1 := make([]int, 10)
146
+ cnt2 := make([]int, 10)
147
+ for i := 0; i < len(secret); i++ {
148
+ a, b := secret[i]-'0', guess[i]-'0'
149
+ if a == b {
150
+ x++
151
+ } else {
152
+ cnt1[a]++
153
+ cnt2[b]++
154
+ }
155
+ }
156
+ for i := 0; i < 10; i++ {
157
+ y += min(cnt1[i], cnt2[i])
158
+ }
159
+ return fmt.Sprintf("%dA%dB", x, y)
160
+ }
161
+
162
+ func min(a, b int) int {
163
+ if a < b {
164
+ return a
165
+ }
166
+ return b
115
167
}
116
168
```
117
169
0 commit comments