프로그래머스/Java

소수 찾기

RunTimeException 2022. 3. 22. 16:02

문제


한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다.

각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이 조각으로 만들 수 있는 소수가 몇 개인지 return 하도록 solution 함수를 완성해주세요.

제한사항

  • numbers는 길이 1 이상 7 이하인 문자열입니다.
  • numbers는 0~9까지 숫자만으로 이루어져 있습니다.
  • "013"은 0, 1, 3 숫자가 적힌 종이 조각이 흩어져있다는 의미입니다.

입출력 예

"17" 3
"011" 2

입출력 예 설명

예제 #1
[1, 7]으로는 소수 [7, 17, 71]를 만들 수 있습니다.

예제 #2
[0, 1, 1]으로는 소수 [11, 101]를 만들 수 있습니다.

  • 11과 011은 같은 숫자로 취급합니다.

답안


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    List<Integer> numList = new ArrayList<Integer>();
    
    public int solution(String numbers) {
        int answer = 0;
        recursive("", numbers);
        
        numList = numList.stream().distinct().collect(Collectors.toList());
        
        Collections.sort(numList, Collections.reverseOrder());
        
        
        for(int index=2; index<= numList.get(0); index++) {
        	boolean flag = true;
        	for(int joker=2; (joker*joker)<= index; joker++) {
        		if(index%joker == 0) {
        			flag =  false;
        			break;
        		}
        	}
        	
        	if(flag) {
        		if(numList.contains(index)) {
        			answer++;
        		}
        	}
        }
        return answer;
    }
    
    public void recursive(String comb, 	String other) {
		if(!comb.equals("")) numList.add(Integer.valueOf(comb));
		
		for(int index=0; index< other.length(); index++) {
			recursive(comb + other.charAt(index), other.substring(0, index) + other.substring(index + 1));
		}
	}
}