https://school.programmers.co.kr/learn/courses/30/lessons/64064
✅ Solution
- permutations를 이용해서 banned_id의 길이만큼 가능한 순열을 모두 구한다.
- 각각 순열을 banned_id와 비교하면서 불량 사용자 배열이 될 수 있는지 체크한다.
- 만약 가능하다면 answer에 넣어준다.
- 이때, 불량 사용자의 아이디 목록 내용이 동일한 경우에는 개수를 더해주면 안되므로 answer에 존재하지 않을 때만 넣어준다.
✅ Code
from itertools import permutations
def check(user, banned_id):
for u, b in zip(user, banned_id):
if len(u) != len(b):
return False
for i in range(len(u)):
if u[i] != b[i] and b[i] != '*':
return False
return True
def solution(user_id, banned_id):
answer = []
candidates = list(permutations(user_id, len(banned_id)))
for candidate in candidates:
if check(candidate, banned_id):
candidate = sorted(candidate)
if candidate not in answer:
answer.append(candidate)
return len(answer)
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 4 - 무지의 먹방 라이브 (Python) (0) | 2023.03.05 |
---|---|
[프로그래머스] Level 3 - [카카오 인턴] 보석 쇼핑 (Python) (0) | 2022.07.07 |
[프로그래머스] Level 3 - 표 편집 (Python) (0) | 2022.07.07 |
[프로그래머스] Level 3 - [1차] 셔틀버스 (Python) (0) | 2022.07.06 |
[프로그래머스] Level 3 - [1차] 추석 트래픽 (Python) (0) | 2022.07.02 |