Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Seol-JY/202511/30 BOJ G5 암호 만들기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static int L, C;
static char[] chars;
static StringBuilder sb = new StringBuilder();
static Set<Character> vowels = Set.of('a', 'e', 'i', 'o', 'u');

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());

chars = new char[C];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < C; i++) {
chars[i] = st.nextToken().charAt(0);
}

Arrays.sort(chars); // 정렬해두면 자연스럽게 사전순으로 탐색

backtrack(0, new char[L], 0);

System.out.print(sb);
}

static void backtrack(int start, char[] password, int depth) {
if (depth == L) {
if (isValid(password)) {
sb.append(password).append('\n');
}
return;
}

for (int i = start; i < C; i++) {
password[depth] = chars[i];
backtrack(i + 1, password, depth + 1);
}
}

static boolean isValid(char[] password) {
int vowelCount = 0;
int consonantCount = 0;

for (char c : password) {
if (vowels.contains(c)) {
vowelCount++;
} else {
consonantCount++;
}
}

return vowelCount >= 1 && consonantCount >= 2;
}
}
```