๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ - #3 ๋ถˆ๋Ÿ‰ ์‚ฌ์šฉ์ž

by applemango2021 2021. 1. 2.

์นด์นด์˜ค๋Š” ์ฐธ ๋ฌธ์ž์—ด ๋ฌธ์ œ๋ฅผ ์ข‹์•„ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

programmers.co.kr/learn/courses/30/lessons/64064

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ถˆ๋Ÿ‰ ์‚ฌ์šฉ์ž

๊ฐœ๋ฐœํŒ€ ๋‚ด์—์„œ ์ด๋ฒคํŠธ ๊ฐœ๋ฐœ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š” ๋ฌด์ง€๋Š” ์ตœ๊ทผ ์ง„ํ–‰๋œ ์นด์นด์˜ค์ด๋ชจํ‹ฐ์ฝ˜ ์ด๋ฒคํŠธ์— ๋น„์ •์ƒ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹น์ฒจ์„ ์‹œ๋„ํ•œ ์‘๋ชจ์ž๋“ค์„ ๋ฐœ๊ฒฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์‘๋ชจ์ž๋“ค์„ ๋”ฐ๋กœ ๋ชจ์•„ ๋ถˆ๋Ÿ‰

programmers.co.kr

์ž๋ฃŒ๊ตฌ์กฐ HashSet๊ณผ String์˜ matches() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ ๊ตฌ๊ธ€๋ง ์•ˆ ํ–ˆ์œผ๋ฉด ๋ชป ํ’€์—ˆ์„ ๊ฑฐ๋‹ค. 

 

์ •๋‹ต์„ ๋„ฃ๋Š” HashSet์˜ ๊ฒฝ์šฐ์—๋Š”, HashSet ์•ˆ์— ๋˜ Set๋‚˜ List๋ฅผ ๋‹ด์•„์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ๋‚˜๋Š” String์ด ์ œ์ผ ํŽธํ•ด์„œ ์ฐพ์•„๋‚ธ user_id๊ฐ’๋“ค์„ StringBuilder ์ด์šฉํ•ด์„œ ํ•ฉ์ณ์ค€ ๋‹ค์Œ์— ๊ทธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ HashSet์— ์ค‘๋ณต์ด ๋‹ด๊ธฐ์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด, "frodo"์™€ "abc123"์ด ๋‹น์ฒจ์—์„œ ์ œ์™ธ๋œ ์‘๋ชจ์ž ์•„์ด๋””๋ผ๋ฉด "abc123 frodo"๋ฅผ resultSet์— ๋‹ด์•„์„œ ๋˜ ๋˜‘๊ฐ™์€ String์ด ๋‹ด๊ธฐ์ง€ ์•Š๋„๋ก ํ–ˆ๋‹ค. 

์•„ ๊ทธ๋ฆฌ๊ณ  ์™œ ๋‹ค๋“ค '*'๋ฅผ '.'๋กœ replaceํ•˜๋Š”์ง€ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, ์ž๋ฐ”์˜ ์ •๊ทœ์‹ ํ‘œํ˜„์—์„œ '.'๊ฐ€ ์ž„์˜์˜ ํ•œ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•ด์„œ ๊ทธ๋Ÿฐ๊ฑฐ์˜€๋‹ค

import java.util.*;

class Solution {
    
    public static HashSet<String> resultSet;
    public static boolean[] checked;
    public int solution(String[] user_id, String[] banned_id) {
        resultSet = new HashSet<String>();
        checked = new boolean[user_id.length];
        
        for(int i=0;i<banned_id.length;i++){
            banned_id[i] = banned_id[i].replace('*','.'); 
        }
        
        dfs(0, user_id, banned_id, new HashSet<String>());
        return resultSet.size();
    }
    
    private static void dfs(int checkIdx, String[] user_id, String[] banned_id, HashSet<String> result){
        
        if(result.size()==banned_id.length){
            ArrayList<String> list = new ArrayList<String>(result);
            Collections.sort(list);
            
            StringBuilder sb = new StringBuilder();
            for(String str : list){
                sb.append(str+" ");
            }
            
            resultSet.add(sb.toString().trim());
            return;
        }

        for(int i=0;i<user_id.length;i++){
            if(checked[i]) continue;
            
            if(user_id[i].matches(banned_id[checkIdx])){
                checked[i] = true;
                result.add(user_id[i]);
                dfs(checkIdx+1, user_id, banned_id, result);
                result.remove(user_id[i]);
                checked[i] = false;
            }
        }
    }
}

 


์ดํด๋ฆฝ์Šค์—์„œ ๊ฐœ๋ฐœํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์— ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋ถ™์—ฌ๋„ฃ๊ธฐ๊ฐ€ ์•ˆ๋ผ์„œ ๋˜ ๋‹ค์‹œ ํ•œ๊ธ€์žํ•œ๊ธ€์ž ํƒ€์ดํ•‘ํ•ด์„œ ์ œ์ถœํ–ˆ๋‹ค. ์ €๋ฒˆ์— ์…ค๋ดค์„ ๋•Œ๋Š” ์•ˆ ๊ทธ๋žฌ๋˜ ๊ฒƒ ๊ฐ™์€๋Ž‹ใ…Žใ…Žใ…Žใ…Ž์ œ์ถœํ–ˆ๋Š”๋ฐ ์ž๊พธ ์ •๋‹ต์ด 0๊ฐœ๋ผ๊ธธ๋ž˜ ์ง„์งœ ๋ˆˆ์•Œ ๋น ์ง€๋Š” ์ค„ ์•Œ์•˜๋‹ค. ์•„ ๊ทธ๋ฆฌ๊ณ  import๋„ ๋‚ด๊ฐ€ ์ง์ ‘ ํ•ด์ฃผ์–ด์•ผ๋˜๋Š” ๊ฑธ ๋˜ ๊ธฐ์–ต ๋ชปํ•˜๊ณ  ์ œ์ถœํ–ˆ๋‹ค๊ฐ€ ํ—ค๋งธ๋‹ค.