본문 바로가기

PS/프로그래머스

[프로그래머스] 불량 사용자 - JAVA(자바)

문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/64064

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

코드:

 

import java.util.*;

class Solution {
    Set<Set<String>> answer = new HashSet<>();
    List<List<String>> userIdList = new ArrayList<>();

    public int solution(String[] user_id, String[] banned_id) {
        for (int i = 0; i < banned_id.length; i++) {
            String banId = banned_id[i];
            List<String> list = new ArrayList<>();
            label : for (String userId : user_id) {
                if (banId.length() == userId.length()) {
                    for (int j = 0; j < banId.length(); j++) {
                        if(banId.charAt(j) == '*') continue;
                        else{
                            if(banId.charAt(j) != userId.charAt(j)) continue label; //다음 userId 확인
                        }
                    }
                    list.add(userId);
                }
            }
            userIdList.add(list);
        }
        // 조합
        recur(0, new HashSet<>());
        return answer.size();
    }

    public void recur(int cur, Set<String> set){
        if (cur == userIdList.size()) {
            answer.add(set);
            return;
        }
        List<String> userIds = userIdList.get(cur);
        for (String userId : userIds) {
            if (!set.contains(userId)) {
                Set<String> tmp = new HashSet<>(set);
                tmp.add(userId);
                recur(cur + 1, tmp);
            }
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Solution sol = new Solution();
        String[] user_id = {
                "frodo", "fradi", "crodo", "abc123", "frodoc"
        };
        String[] banned_id = {
                "fr*d*", "abc1**"
        };
        int result = 2;
        int answer = sol.solution(user_id, banned_id);
        System.out.println(answer == result);
    }
}