import java.util.*;
public class Solution {
static int N, M;
static boolean[][] matched;
static Set<Integer> visitedBits;
static int res;
public int solution(String[] user_id, String[] banned_id) {
res = 0;
N = banned_id.length;
M = user_id.length;
matched = new boolean[N][M];
visitedBits = new HashSet<>();
for (int i = 0; i < N; i++) {
char[] bannedId = banned_id[i].toCharArray();
for (int j = 0; j < M; j++) {
if (bannedId.length != user_id[j].length()) {
continue;
}
char[] userId = user_id[j].toCharArray();
boolean isMatched = true;
for (int k = 0; k < userId.length; k++) {
if (bannedId[k] != '*' && bannedId[k] != userId[k]) {
isMatched = false;
break;
}
}
if (isMatched) {
matched[i][j] = true;
}
}
}
go(0, 0);
return res;
}
public void go(int idx, int bit) {
if (idx >= N) {
res++;
return;
}
for (int j = 0; j < M; j++) {
if (!matched[idx][j] || ((bit & (1 << j)) > 0)) {
continue;
}
int nextBit = bit | 1 << j;
if (visitedBits.contains(nextBit)) {
continue;
}
visitedBits.add(nextBit);
go(idx + 1, nextBit);
}
}
}