Repository navigation
Expand file tree
/
Copy pathHW_64064.java
More file actions
42 lines (36 loc) ยท 1.54 KB
/
Copy pathHW_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); // ์ ํ ์ทจ์
}
}
}
}