๐Ÿ”Žํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์†Œ์ˆ˜ ์ฐพ๊ธฐ

๋ฐ•๋ฏผ์šฐยท2023๋…„ 7์›” 17์ผ
0
post-custom-banner

๋ฌธ์ œ: 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;
}
profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 18์ผ

์†Œ์ค‘ํ•œ ์ •๋ณด ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ
comment-user-thumbnail
2023๋…„ 7์›” 18์ผ

์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ