조합

박진은·2023년 7월 3일
0

코테

목록 보기
37/44
import java.util.*;

class Solution {
    Set<Set<String>> sets = new HashSet<>();

    public boolean judge(String a, String b) {
        if (a.length() != b.length())
            return false;

        for (int i = 0; i < a.length(); i++) {
            char ca = a.charAt(i);
            char cb = b.charAt(i);
            if (ca != cb && ca != '*' && cb != '*')
                return false;
        }

        return true;
    }

    public void combination(String[] user_id, String[] banned_id, int index, Set<String> selected) {
        if (index == banned_id.length) {
            sets.add(new HashSet<>(selected));
            return;
        }

        for (int j = 0; j < user_id.length; j++) {
            String a = banned_id[index];
            String b = user_id[j];

            if (!a.equals("-") && judge(a, b) && !selected.contains(b)) {
                selected.add(b);
                combination(user_id, banned_id, index + 1, selected);
                selected.remove(b);
            }
        }
    }

    public int solution(String[] user_id, String[] banned_id) {
        combination(user_id, banned_id, 0, new HashSet<>());
        return sets.size();
    }
}

위 코드에서 user_id, 와 banned_id 간의 조합을 만들어야 해서 combination 이라는 재귀함수를 만들어서 호출했다.
호드를 뭔가 외워야되나 싶다.

profile
코딩

0개의 댓글