# ★★☆☆☆ 소수 찾기

# 구현코드

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int solution(String numbers) {
        int[] permutation = new int[numbers.length()];
        for(int i = 0; i < permutation.length; i++)
            permutation[i] = i;

        int count = 0;
        Set<Integer> set = new HashSet<>();
        do{
            int[] inputNumbers = new int[numbers.length()];
            for(int i = 0; i < permutation.length; i++){
                inputNumbers[i] = numbers.charAt(permutation[i]) - '0';
            }
            boolean[] checked = new boolean[numbers.length()];
            count += recursiveCheckNumber(inputNumbers,checked,0,set);
        }while (nextPermutation(permutation));

        return count;
    }

    private boolean nextPermutation(int[] numbers){
        int prevIndex;
        int n = numbers.length;
        int nextIndex = n-1;
        int changeIndex = -1;

        for(int i = n-2; i >= 0; i--){
            prevIndex = nextIndex;
            nextIndex = i;

            if(numbers[nextIndex] < numbers[prevIndex]){
                changeIndex = nextIndex;
                break;
            }
        }

        if(changeIndex == -1)
            return false;

        for(int i = n-1; i > changeIndex; i--){
            if(numbers[i] > numbers[changeIndex]){
                swap(numbers, i, changeIndex);
                break;
            }
        }
        while(++changeIndex < --n){
            swap(numbers,changeIndex,n);
        }
        return true;
    }

    private void swap(int[] numbers, int a, int b){
        int temp = numbers[a];
        numbers[a] = numbers[b];
        numbers[b] = temp;
    }

    public int recursiveCheckNumber(int[] numbers, boolean[] checked, int checkedNumber,Set<Integer> set) {
        if(checkedNumber == numbers.length){
            String number = "";
            for(int i = 0; i < numbers.length; i++){
                if(checked[i])
                    number = number + numbers[i];
            }

            if(number.equals(""))
                return 0;

            if(set.contains(Integer.parseInt(number)))
                return 0;
            set.add(Integer.parseInt(number));

            if(isPrime(Integer.parseInt(number)))
                return 1;
            else
                return 0;
        }

        checked[checkedNumber] = true;
        int a = recursiveCheckNumber(numbers,checked,checkedNumber+1,set);
        checked[checkedNumber] = false;
        int b = recursiveCheckNumber(numbers,checked,checkedNumber+1,set);
        return a + b;
    }

    public boolean isPrime(int number){
        if(number == 1 || number == 0)
            return false;
        for(int i = 2; i*i <= number; i++){
            if(number % i == 0)
                return false;
        }
        return true;
    }
}
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98