-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
32 lines (27 loc) · 873 Bytes
/
res.js
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
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function(secret, guess) {
const arrLen = secret.length;
let strA = 0;
let strB = 0;
let secMap = new Array(10);
let gueMap = new Array(10);
for (let index = 0; index < arrLen; index++) {
if (secret[index] === guess[index]) {
strA++;
} else {
const secNum = parseInt(secret[index]);
const guenum = parseInt(guess[index]);
(secMap[secNum] ? secMap[secNum] += 1 : secMap[secNum] = 1);
(gueMap[guenum] ? gueMap[guenum] += 1 : gueMap[guenum] = 1);
}
}
for (let index = 0; index < 10; index++) {
strB += (secMap[index] && gueMap[index] && (secMap[index] > gueMap[index] ? gueMap[index] : secMap[index]) || 0);
}
// console.log(secMap, gueMap);
return `${strA}A${strB}B`;
};