BOJ1759 암호 만들기
골드V | 백준 1759 | Python3 파이썬 풀이
길이가 L인 모든 조합을 Python itertools
라이브러리 내장 함수인 combinations
로 구한 뒤, 각 원소를 정렬해주고, 리스트도 정렬해준다.
생성된 조합 원소들 중 모음이 1개 이상, 자음이 2개 이상 조건에 맞는 원소만 출력하면 된다.
Python의 itertools
를 사용하면, 순열, 조합을 아주 편리하게 구할 수 있다.
import sys
import itertools
input = sys.stdin.readline
L, C = map(int, input().split())
chars = list(input().split())
# 길이가 L인 조합을 정렬해서 리스트로 저장한다
result = (list(map(lambda x : sorted(list(x)), list(itertools.combinations(chars, L)))))
result.sort()
for r in result:
count = 0
for rr in r:
# 모음의 개수를 센다
if rr in ['a', 'e', 'i', 'o', 'u']:
count += 1
# 모음이 1개 이상, 자음이 2개 이상이라면 출력
if 0 < count < L - 1:
print(''.join(r))