https://programmers.co.kr/learn/courses/30/lessons/64064
Code
1
2
3
4
5
6
7
8
9
10
11
12
import re
from itertools import product
def solution(user_id, banned_id):
matched_id = (
[i for i, id in enumerate(user_id) if re.match(f"^{p.replace('*', '.')}$", id)]
for p in banned_id
)
# selected_id = [set(id) for id in product(*matched_id)] # <- 시간초과
selected_id = (set(id) for id in product(*matched_id)) # <- 통과
selected_id = set(tuple(id) for id in selected_id if len(id) == len(banned_id))
return len(selected_id)
Complexity
$O(|\text{user_id}|!)$
Reminder
- Generator를 사용할 수 있는 경우엔 사용!
PREVIOUSEtc