[python] 프로그래머스 불량 사용자

Youngseo Lee·2024년 9월 23일

완전탐색

목록 보기
3/3

프로그래머스 불량 사용자

https://school.programmers.co.kr/learn/courses/30/lessons/64064

문제

풀이

from itertools import permutations

def solution(user_id, banned_id):
    count = len(banned_id)
    combinations = permutations(user_id, count)
    answer = set()
    
    # 각 조합에 대해 확인 
    for com in combinations:
        found = True
        
        # banned_id와 비교하기 위한 index 설정
        for idx in range(count):
            cid = com[idx]
            bid = banned_id[idx]
            
            # 길이가 다르면 바로 제외
            if len(cid) != len(bid):
                found = False
                break
            
            # 한 글자씩 비교
            for i in range(len(cid)):
                if bid[i] == '*':
                    continue
                if bid[i] != cid[i]:
                    found = False
                    break
            
            if not found:
                break
        
        if found:
            answer.add(tuple(sorted(com)))
    
    return len(answer)

📌 주의

user_id의 배열의 크기는 1이상 8이하라고 명시되어 있으므로 그냥 permutations 을 썼다. for idx in range(count): 를 할 생각을 못해서 시간이 오래걸렸다.

profile
leenthepotato

0개의 댓글