잘못된 정보가 있다면, 꼭 댓글로 알려주세요(비로그인 익명도 가능).

여러분의 피드백이 저와 방문자 모두를 올바른 정보로 인도할 수 있습니다.

감사합니다. -현록

후원해주실 분은 여기로→

현록의 기록저장소

[Lv3] 불량 사용자 본문

Problem Solving/programmers

[Lv3] 불량 사용자

현록 2021. 4. 22. 01:01

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

 

코딩테스트 연습 - 불량 사용자

개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다. 이런 응모자들을 따로 모아 불량

programmers.co.kr

문제 설명

개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는

최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다.

이런 응모자들을 따로 모아 불량 사용자라는 이름으로 목록을 만들어서

당첨 처리 시 제외하도록 이벤트 당첨자 담당자인 "프로도" 에게 전달하려고 합니다.

이 때 개인정보 보호을 위해 사용자 아이디 중 일부 문자를 '*' 문자로 가려서 전달했습니다.

가리고자 하는 문자 하나에 '*' 문자 하나를 사용하였고 아이디 당 최소 하나 이상의 '*' 문자를 사용하였습니다.

"무지"와 "프로도"는 불량 사용자 목록에 매핑된 응모자 아이디를 제재 아이디 라고 부르기로 하였습니다.

예를 들어, 이벤트에 응모한 전체 사용자 아이디 목록이 다음과 같다면

응모자 아이디

frodo

fradi

crodo

abc123

frodoc

다음과 같이 불량 사용자 아이디 목록이 전달된 경우,

불량 사용자

fr*d*

abc1**

불량 사용자에 매핑되어 당첨에서 제외되어야 야 할 제재 아이디 목록은 다음과 같이 두 가지 경우가 있을 수 있습니다.

제재 아이디

frodo

abc123

제재 아이디

fradi

abc123

이벤트 응모자 아이디 목록이 담긴 배열 user_id와 불량 사용자 아이디 목록이 담긴 배열 banned_id가 매개변수로 주어질 때,

당첨에서 제외되어야 할 제재 아이디 목록은 몇가지 경우의 수가 가능한 지 return 하도록 solution 함수를 완성해주세요.

[제한사항]

  • user_id 배열의 크기는 1 이상 8 이하입니다.
  • user_id 배열 각 원소들의 값은 길이가 1 이상 8 이하인 문자열입니다.
    • 응모한 사용자 아이디들은 서로 중복되지 않습니다.
    • 응모한 사용자 아이디는 알파벳 소문자와 숫자로만으로 구성되어 있습니다.
  • banned_id 배열의 크기는 1 이상 user_id 배열의 크기 이하입니다.
  • banned_id 배열 각 원소들의 값은 길이가 1 이상 8 이하인 문자열입니다.
    • 불량 사용자 아이디는 알파벳 소문자와 숫자, 가리기 위한 문자 '*' 로만 이루어져 있습니다.
    • 불량 사용자 아이디는 '*' 문자를 하나 이상 포함하고 있습니다.
    • 불량 사용자 아이디 하나는 응모자 아이디 중 하나에 해당하고 같은 응모자 아이디가 중복해서 제재 아이디 목록에 들어가는 경우는 없습니다.
  • 제재 아이디 목록들을 구했을 때 아이디들이 나열된 순서와 관계없이 아이디 목록의 내용이 동일하다면 같은 것으로 처리하여 하나로 세면 됩니다.

[입출력 예]

user_id

banned_id

result

["frodo", "fradi", "crodo", "abc123", "frodoc"]

["fr*d*", "abc1**"]

2

["frodo", "fradi", "crodo", "abc123", "frodoc"]

["*rodo", "*rodo", "******"]

2

["frodo", "fradi", "crodo", "abc123", "frodoc"]

["fr*d*", "*rodo", "******", "******"]

3

입출력 예에 대한 설명입출력 예 #1

문제 설명과 같습니다.

입출력 예 #2

다음과 같이 두 가지 경우가 있습니다.

제재 아이디

frodo

crodo

abc123

제재 아이디

frodo

crodo

frodoc

입출력 예 #3

다음과 같이 세 가지 경우가 있습니다.

제재 아이디

frodo

crodo

abc123

frodoc

제재 아이디

fradi

crodo

abc123

frodoc

제재 아이디

fradi

frodo

abc123

frodoc


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Solution {
    public void calc(HashSet<Integer> set, ArrayList<Integer>[] variations, int[] selected, int index) {
        if (index >= variations.length) {
            Arrays.sort(selected);
            int hash = 0;
            for (int i = 0; i < selected.length; ++i) {
                hash += selected[i] * Math.pow(10, i);
            }
            set.add(hash);
            return;
        }
        for (Integer able : variations[index]) {
            boolean isAble = true;
            for (int i = 0; i < index; ++i) {
                if (able == selected[i]) {
                    isAble = false;
                    break;
                }
            }
            if (isAble == true) {
                int[] newSelected = selected.clone();
                newSelected[index] = able;
                calc(set, variations, newSelected, index + 1);
            }
        }
    }

    public int solution(String[] user_id, String[] banned_id) {
        ArrayList<Integer>[] ables = new ArrayList[banned_id.length];
        for (int i = 0; i < banned_id.length; ++i) {
            ables[i] = new ArrayList<>(user_id.length);
            String regex = banned_id[i].replaceAll("\\*", "\\.");
            regex = String.format("^%s$", regex);
            for (int j = 0; j < user_id.length; ++j) {
                Matcher matcher = Pattern.compile(regex).matcher(user_id[j]);
                if (matcher.find()) {
                    ables[i].add(j);
                }
            }
        }
        HashSet<Integer> alreadyDone = new HashSet<>(ables.length);
        while (true) {
            boolean isAllDone = true;
            for (int i = 0; i < ables.length; ++i) {
                if (ables[i] != null && ables[i].size() == 1) {
                    isAllDone = false;
                    alreadyDone.add(i);
                    for (int j = 0; j < ables.length; ++j) {
                        if (j == i) {
                            continue;
                        }
                        if (ables[j] != null) {
                            ables[j].remove(Integer.valueOf(ables[i].get(0)));
                        }
                    }
                    ables[i] = null;
                }
            }
            for (int i = 0; i < ables.length; ++i) {
                if (ables[i] != null) {
                    ArrayList<Integer> sames = new ArrayList<>(ables[i].size());
                    sames.add(i);
                    for (int j = 0; j < ables.length; ++j) {
                        if (j == i) {
                            continue;
                        }
                        if (ables[j] != null && ables[j].size() == ables[i].size() && ables[j].containsAll(ables[i])) {
                            sames.add(j);
                        }
                    }
                    if (sames.size() == ables[i].size()) {
                        isAllDone = false;
                        int[] willDel = ables[i].stream().mapToInt(Integer::intValue).toArray();
                        for (Integer index : sames) {
                            alreadyDone.add(index);
                            ables[index] = null;
                        }
                        for (int j = 0; j < ables.length; ++j) {
                            if (ables[j] != null) {
                                for (int k = 0; k < willDel.length; ++k) {
                                    ables[j].remove(Integer.valueOf(willDel[k]));
                                }
                            }
                        }
                    }
                }
            }
            if (isAllDone == true) {
                break;
            }
        }
        if (alreadyDone.size() == ables.length) {
            return 1;
        }
        ArrayList<Integer>[] variations = new ArrayList[ables.length - alreadyDone.size()];
        int index = 0;
        for (int i = 0; i < ables.length; ++i) {
            if (ables[i] != null) {
                variations[index++] = ables[i];
            }
        }
        HashSet<Integer> set = new HashSet<>();
        int[] selected = new int[variations.length];
        calc(set, variations, selected, 0);
        return set.size();
    }
}

 

 

'Problem Solving > programmers' 카테고리의 다른 글

[Lv4] 징검다리 - 이분탐색(BS)  (0) 2023.04.17
[Lv3] 기둥과 보 설치  (0) 2021.04.22
[Lv3] 추석 트래픽  (0) 2021.04.21
[Lv3] 순위  (0) 2021.04.20
[Lv3] 셔틀버스  (0) 2021.04.20
Comments

잘못된 정보가 있다면, 꼭 댓글로 알려주세요(비로그인 익명도 가능).

여러분의 피드백이 저와 방문자 모두를 올바른 정보로 인도할 수 있습니다.

감사합니다. -현록

후원해주실 분은 여기로→