문제링크 : https://programmers.co.kr/learn/courses/30/lessons/64064
class Solution {
public static boolean visited[];
public static boolean bitmask[];
public static int ans;
public static String g_banned_id[];
public static String g_user_id[];
public static boolean availMatching(String str, String pattern){
for(int i=0;i<str.length();i++){
if(pattern.charAt(i)=='*') continue;
if(str.charAt(i) != pattern.charAt(i)){
return false;
}
}
return true;
}
public static void dfs(int idx){
if(idx == g_banned_id.length){
int mask = 0;
for(int i=0;i< g_user_id.length;i++){
if(visited[i]){
mask |= (1 << i);
}
}
if(bitmask[mask] != true){
bitmask[mask] = true;
ans++;
}
return;
}
boolean suc = false;
for(int i=0;i<g_user_id.length;i++){
if(visited[i]) continue;
if(g_user_id[i].length() != g_banned_id[idx].length()) continue;
if(availMatching(g_user_id[i],g_banned_id[idx])){
visited[i] = true;
dfs(idx+1);
visited[i] = false;
}
}
}
public static int solution(String[] user_id, String[] banned_id) {
g_user_id = new String[user_id.length];
g_banned_id = new String[banned_id.length];
visited = new boolean[user_id.length];
bitmask = new boolean[(1<<8)];
for(int i=0;i<user_id.length;i++){
g_user_id[i] = user_id[i];
}
for(int i=0;i< banned_id.length;i++){
g_banned_id[i] = banned_id[i];
}
dfs(0);
return ans;
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 2 X N 타일링 / JAVA (0) | 2022.02.18 |
---|---|
[프로그래머스] GPS / JAVA (0) | 2022.02.18 |
[프로그래머스] 보석 쇼핑 / JAVA (0) | 2022.02.18 |
[프로그래머스] 순위 / JAVA (0) | 2022.02.18 |
[프로그래머스] 네트워크 / JAVA (0) | 2022.02.18 |