완전 탐색
, 소수
https://school.programmers.co.kr/learn/courses/30/lessons/42839
import java.util.*;
class Solution {
static boolean[] primes = new boolean[10_000_000];
static boolean[] checkedPastedValue = new boolean[10_000_000];
static boolean[] visited;
static int answer = 0;
public int solution(String numbers) {
visited = new boolean[numbers.length()];
calcPrime();
dfs(numbers, 0, "");
return answer;
}
public void calcPrime() {
Arrays.fill(primes, true);
primes[0] = primes[1] = false;
for(int i=2; i<Math.sqrt(primes.length); i++) {
if(primes[i]) {
for(int j=i*2; j<primes.length; j+=i) {
primes[j] = false;
}
}
}
}
public void dfs(String numbers, int depth, String pastedStr) {
if(depth == numbers.length()) {
return ;
}
for(int i=0; i<numbers.length(); i++) {
if(!visited[i]) {
int pastedValue = Integer.parseInt(pastedStr + numbers.charAt(i));
if(primes[pastedValue] && !checkedPastedValue[pastedValue]) {
answer++;
checkedPastedValue[pastedValue] = true;
}
visited[i] = true;
dfs(numbers, depth + 1, pastedStr + numbers.charAt(i));
visited[i] = false;
}
}
}
}
20분