๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/42839
์นดํ ๊ณ ๋ฆฌ: ์์ ํ์
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ๊ณ ๋์ Kit, https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit
์ฃผ์ด์ง ๋ฐฐ์ด์ ๊ฐ ์๋ฅผ ๋ถ์ฌ ๋ง๋ค ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฌ๊ท๋ฅผ ์ด์ฉํด ํ์ํ๊ณ , ์์์ธ์ง ํ๋ณํด์ ์ด ์์์ ๊ฐ์๋ฅผ ๋ฆฌํดํ๋ค.
const isPrime = (num) => {
if (num < 2) {
return false;
}
if (num == 2) {
return true;
}
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
};
const dfs = (set, arr, str) => {
if (arr.length >= 1) {
for (let i = 0; i < arr.length; i++) {
let newStr = str + arr[i];
let newArr = [...arr];
newArr.splice(i, 1);
if (isPrime(parseInt(newStr))) {
set.add(parseInt(newStr));
}
dfs(set, newArr, newStr);
}
}
};
function solution(numbers) {
let nums = numbers.split("");
let set = new Set();
dfs(set, nums, "");
return set.size;
}
์์คํ ์ ๋ณด ๊ฐ์ฌ๋๋ฆฝ๋๋ค!