private Set<Set<String>> result;
public int solution(String[] userId, String[] bannedId) {
result = new HashSet<>();
dfs(new HashSet<>(), 0, userId, bannedId);
return result.size();
}
private void dfs(Set<String> currentSet, int index, String[] userIds, String[] bannedId) {
if (index == bannedId.length) {
result.add(new HashSet<>(currentSet));
return;
}
for (String userId : userIds) {
if (!currentSet.contains(userId) && isMatching(userId, bannedId[index])) {
currentSet.add(userId);
dfs(currentSet, index + 1, userIds, bannedId);
currentSet.remove(userId);
}
}
}
private boolean isMatching(String userId, String bannedId) {
if (userId.length() != bannedId.length()) {
return false;
}
for (int i = 0; i < bannedId.length(); i++) {
if (bannedId.charAt(i) == '*') {
continue;
}
if (userId.charAt(i) != bannedId.charAt(i)) {
return false;
}
}
return true;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/64064