[ 알고리즘 ]/Brute Force

[백준] 1759. 암호 만들기

황찌옹 2022. 7. 2. 23:08

 

📌문제

https://www.acmicpc.net/problem/1759

 

🎖️난이도

 

✔️풀이

from itertools import combinations
L, _ = map(int, input().split())
str_list = input().split()
result_list = []

def isPossible(comb):
    if len(set(comb) & set(('a', 'e', 'i', 'o', 'u')))!= 0 and len(set(comb) - set(('a', 'e', 'i', 'o', 'u')))>=2:
           return True
    return False

for comb in combinations(str_list, L):
    if isPossible(comb):
        result_list.append(''.join(sorted(comb)))

for item in sorted(result_list):
    print(item)

 

🧠노트

itertools의 combinations를 이용해, 암호로 가능한 모든 조합을 구한 후 result_list에 저장한다. 이 때, set의 교집합(&) 및 차집합(-) 연산자를 이용하면 모음 및 자음 개수를 셀 수 있다.

 

🔍참고

-