문제 링크
문제 풀이
user_id가 banned_id에서 여러 개에 걸릴 수 있다. 그래서 NxN의 완전탐색으로 풀면 경우의 수를 놓칠 수 있다.
재귀(DFS)로 user_id와 banned_id가 매칭된 상태를 다음 재귀로 넘겨주며
가능한 상황을 다 count해야 한다.
전체 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| const checkWord = (a, b) => new RegExp("^" + b.split("*").join(".") + "$").test(a);
function solution(user_id, banned_id) { let temp = {};
const compare = (users, ban, depth) => { if (depth < banned_id.length) users.forEach((user, i) => { if (checkWord(user, ban[0])) { const userCopy = users.slice(); const banCopy = ban.slice(); userCopy.splice(i, 1); banCopy.splice(0, 1); compare(userCopy, banCopy, depth + 1); } }); else temp[users] = true; };
compare(user_id, banned_id, 0);
return Object.keys(temp).length; }
|