https://programmers.co.kr/learn/courses/30/lessons/42841
function solution(baseball) {
var answer = 0;
var candidate = '';
//possible numbers
for (var i=123; i<=987; i++){
candidate = i.toString();
if((candidate.charAt(1)==="0")
||(candidate.charAt(2)==="0")
||(candidate.charAt(0)===candidate.charAt(1))
||(candidate.charAt(0)===candidate.charAt(2))
||(candidate.charAt(1)===candidate.charAt(2))
) {
continue;
}
//check strike and ball
//check strike
var check = true;
for(var j=0; j<baseball.length; j++){
var baseballNumber = String(baseball[j][0]);
var strike = 0;
for(var k=0; k<3; k++){
if(candidate[k] === baseballNumber.charAt(k)){
strike = strike + 1;
}
}
if(strike !== baseball[j][1]){
check = false;
break;
}
// check ball
var ball = 0;
for(var k=0; k<3; k++){
if(candidate.includes(baseballNumber.charAt(k))){
// if(candidate.indexOf(baseballNumber.charAt(k)) > 0 ){
ball = ball + 1;
}
}
ball = ball - strike;
if(ball !== baseball[j][2]){
check = false;
break;
}
}
if(check){
answer = answer + 1;
}
}
return answer;
}
완전탐색