[알고리즘] 백준 - 1759 ( 암호 만들기 ) / 자바

배고픈메꾸리·2021년 3월 16일
0

알고리즘

목록 보기
63/128

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static int L;
	static int C;
	static String[] arr;
	static StringBuilder sb= new StringBuilder(); ;
	
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		L = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		arr = new String[C];
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < C; i++) {
			arr[i] = st.nextToken();
		}
		
		Arrays.sort(arr);
		solve(0,"");
		System.out.println(sb);
	}

	public static void solve(int index, String str) {
		if (str.length() == L) {
			if(isPossible(str)) {
				sb.append(str).append("\n");
			}
			return;
		}
		if (index >= C) {
			return;
		}
		solve(index + 1, str + arr[index]);
		solve(index + 1, str);
	}

	public static boolean isPossible(String str) {
		int vowel = 0;
		int consonant = 0;
		char c;
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
				vowel++;
			} else {
				consonant++;
			}
		}
		if (vowel >= 1 && consonant >= 2) {
			return true;
		}
		return false;
	}
	
}

profile
FE 개발자가 되자

0개의 댓글