import java.util.*;
class solve {
static HashSet<HashSet<String>> hs = new HashSet<>();
String[] user_id;
String[] banned_id;
boolean[] check;
public int solution(String[] user_id, String[] banned_id) {
this.user_id = user_id;
this.banned_id = banned_id;
check = new boolean[user_id.length];
perm(0, new HashSet<String>());
int answer = hs.size();
return answer;
}
public void perm(int depth, HashSet<String> set) {
if(depth == banned_id.length) {
hs.add(set);
return;
}
for(int i = 0; i < user_id.length; i++) {
if(check[i]) continue;
if(banned_id[depth].length() != user_id[i].length()) continue;
if(matched(user_id[i], banned_id[depth])) {
set.add(user_id[i]);
check[i] = true;
perm(depth + 1, set);
set.remove(user_id[i]);
check[i] = false;
}
}
}
public boolean matched(String user, String banned) {
for(int i = 0; i < user.length(); i++) {
if(banned.charAt(i) == '*') continue;
if(user.charAt(i) != banned.charAt(i)) return false;
}
return true;
}
}
public class Solution {
public static void main(String[] args) {
String[] user_id = {"frodo", "fradi", "crodo", "abc123", "frodoc"};
String[] banned_id = {"fr*d*", "*rodo", "******", "******"};
solve s = new solve();
System.out.println(s.solution(user_id, banned_id));
}
}
찾아본 바로는 HashSet 으로 저장하는 부분이 문제인데,
저장하고 중복을 거르는 부분만 다르게 바꿔주면 되더라구요.
혹시 도와주실분 있나요 ㅠ
저도 문제 있었는데 ex)user_id : ["12345", "12453", "aaaaa"] ban_id : ["*****", "*****" ] 테스트 케이스 돌려보세요 이거 해결하고 해결했습니다
감사합니다!!!
perm에서 hs.add(set);를 hs.add(new HashSet<>(set)); 으로 바꿔보세요
저도 같은 이유로 3번만 에러 났었는데 통과되네요
차이점은 잘 모르겠는데 혹시 아시는 분 계실까요,,,
그러네요.. 통과했네요 ㅠ 왜그럴까요..
주소값 그 자체가 들어가버리기 때문에, 나중에 set객체를 수정해버리면 hs안에 있는 set객체가 수정되어 버리는 경우가 생기기 때문 아닐까요? 깊은복사, 얕은복사 개념이 들어가는거 같아요
감사합니다!