-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_64064.java
More file actions
42 lines (36 loc) · 1.54 KB
/
HW_64064.java
File metadata and controls
42 lines (36 loc) · 1.54 KB
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
import java.util.*;
class HW_64064 {
static Set<Set<String>> resultSet; // 제재 아이디 목록을 저장할 집합
static List<List<String>> matchedList; // banned_id와 매칭되는 user_id 목록
public int solution(String[] user_id, String[] banned_id) {
resultSet = new HashSet<>();
matchedList = new ArrayList<>();
for (int i = 0; i < banned_id.length; i++) { // banned_id와 매칭되는 user_id 찾기
List<String> matched = new ArrayList<>();
String regex = banned_id[i].replace("*", ".");
for (int j = 0; j < user_id.length; j++) {
if (user_id[j].matches(regex)) { // 정규식으로 매칭 확인
matched.add(user_id[j]);
}
}
matchedList.add(matched);
}
backtrack(0, new HashSet<>());
return resultSet.size();
}
private void backtrack(int depth, Set<String> selected) {
if (depth == matchedList.size()) { // 모든 banned_id를 처리하면
resultSet.add(new HashSet<>(selected)); // 결과 집합에 추가
return;
}
List<String> currentMatched = matchedList.get(depth);
for (int i = 0; i < currentMatched.size(); i++) {
String user = currentMatched.get(i);
if (!selected.contains(user)) { // 중복 방지
selected.add(user); // 선택
backtrack(depth + 1, selected); // 재귀
selected.remove(user); // 선택 취소
}
}
}
}