forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYJ_64064.java
More file actions
48 lines (41 loc) · 1.45 KB
/
YJ_64064.java
File metadata and controls
48 lines (41 loc) · 1.45 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
43
44
45
46
47
48
import java.util.*;
//user_id 배열의 크기는 1 이상 8 이하 > 8!
public class YJ_64064 {
HashSet<String> set = new HashSet<>();
String[] userIds;
String[] bannedIds;
boolean[] visited;
public int solution(String[] user_id, String[] banned_id) {
for(int i=0; i<banned_id.length; i++){
banned_id[i] = banned_id[i].replace('*','.');
}
userIds = user_id;
bannedIds = banned_id;
visited = new boolean[user_id.length];
permutation(0, "");
return set.size();
}
//유저 수(n) 중에서 제재 수(r) 만큼 뽑기
private void permutation(int index, String ids){
//r만큼 다 뽑았을 경우
if(index == bannedIds.length){
String[] combination = ids.split("\\s");
Arrays.sort(combination);
StringBuilder bannedCombination = new StringBuilder();
for (String id : combination) {
bannedCombination.append(id);
}
set.add(bannedCombination.toString());
return;
}
for(int i=0; i<userIds.length; i++){
//이미 방문했거나 제재 목록과 형식 일치하지 않을 경우 패스
if(visited[i] || !userIds[i].matches(bannedIds[index])){
continue;
}
visited[i] = true;
permutation(index+1, ids + " " + userIds[i]);
visited[i] = false;
}
}
}