[백준] 1759 - 암호 만들기 [JAVA]

 

코드

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

public class Main {

    static char[] input, output;
    static int L, C;

    static void makeCode(int depth, int start) {
        if (depth == L) {
            if (isValid()) {
                System.out.println(output);
            }
            return;
        }

        for (int i = start; i < C; i++) {
            output[depth] = input[i];
            makeCode(depth + 1, i + 1);
        }

    }

    static boolean isValid() {
        int mo = 0;
        int ja = 0;

        for (char c : output) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') mo++;
            else ja++;
        }

        // 조건 만족시 true
        if (1 <= mo && 2 <= ja) return true;
        return false;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());

        L = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());

        input = new char[C];
        output = new char[L];

        st = new StringTokenizer(br.readLine());

        for (int i = 0; i < C; i++) {
            input[i] = st.nextToken().charAt(0);
        }

        Arrays.sort(input);

        makeCode(0, 0);

    }
}