ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/64064

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

[ํ’€์ด]

์šฐ์„  banned_id[]๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ banned_id์— ๋Œ€์‘๋  ์ˆ˜ ์žˆ๋Š” user_id๋“ค์˜ index๊ฐ’์„ bandList.get(index)์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ban์ด ๊ฐ€๋Šฅํ•œ ์ด๋ฆ„์ธ์ง€ boolean banned(String ban, String name)์„ ํ†ตํ•ด *๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋“ค์„ ๋น„๊ตํ•˜๋ฉฐ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ1์„ ์˜ˆ์‹œ๋กœ ์‚ดํŽด๋ณด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด banList๊ฐ€ ์ดˆ๊ธฐํ™” ๋ฉ๋‹ˆ๋‹ค.

[[0, 1], [3]]

 

 

๋‹ค์Œ์œผ๋กœ void makeSet(String[] user_id, String[] banned_id, int cnt, String set, boolean[] visit)์„ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ๋“ค์„ ๋ฝ‘์•„์„œ ๋งคํ•‘์ด ์™„๋ฃŒ๋œ user_id์˜ ์กฐํ•ฉ์„ ๊ฐ index๊ฐ€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ String ํ˜•ํƒœ๋กœ ์ €์žฅํ•œ ๊ฐ’์„ names์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ names๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋”๋ผ๋„, ๊ฐ™์€ ์ด๋ฆ„๋“ค์ด ์ €์žฅ๋œ ๊ฒฝ์šฐ ์ค‘๋ณต์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

(index๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด Arrays.sort()๋ฅผ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.)

 

๋งคํ•‘์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ์˜ˆ์‹œ1์˜ names๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

[03, 13]

๋งˆ์ง€๋ง‰์œผ๋กœ names์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด answer์— ์ €์žฅํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

import java.util.*;
class Solution {
    public static ArrayList<String> names = new ArrayList<String>();
    public static ArrayList<ArrayList<Integer>> banList = new ArrayList<ArrayList<Integer>>();
    public static int N;
    public int solution(String[] user_id, String[] banned_id) {
        int answer = 0;
        N = banned_id.length;
        for(int i=0;i<N;i++) {
            banList.add(new ArrayList<Integer>());
            for(int j=0;j<user_id.length;j++) {
                if(banned(banned_id[i], user_id[j])) {
                    banList.get(i).add(j);
                }
            }
        }
        boolean[] visit = new boolean[user_id.length];
        makeSet(user_id, banned_id, 0, "", visit);
        answer = names.size();
        return answer;
    }
    public static void makeSet(String[] user_id, String[] banned_id, int cnt, String set, boolean[] visit) {
        if(cnt == N) {
            char[] StringtoChar = set.toCharArray();
            Arrays.sort(StringtoChar);
            String str = new String(StringtoChar);
            if(!names.contains(str)) {
                names.add(str);
            }
            return;
        }
        for(Integer i:banList.get(cnt)) {
            if(visit[i]) {
                continue;
            }
            visit[i] = true;
            String nextStr = set + i;
            makeSet(user_id, banned_id, cnt + 1, nextStr, visit);
            visit[i] = false;
        }
    }
    public static boolean banned(String ban, String name) {
        int len = ban.length();
        if(len != name.length()) {
            return false;
        }
        for(int i=0;i<len;i++) {
            if(ban.charAt(i) != '*' && ban.charAt(i) != name.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}