백준 1759 암호 만들기

이상현·2021년 6월 29일
0

알고리즘_문제풀이

목록 보기
29/45
post-thumbnail

암호 만들기

문제는 백준에서 확인 할 수 있다.


✔ 접근방법

가능한 조합을 구한 후, 조건에 맞는지 확인


✔ 코드

from itertools import combinations

def solution(charset):
    answer = []

    global L, C
    combi = list(combinations(charset, L))
    must_have = ['a','e','i','o','u']
    
    for c in combi:
        # 자음 모음 갯수 확인
        consonant_cnt = 0
        vowel_cnt = 0

        cur = ''.join(c)
        
        # 하나 이상의 모음이 포함되었는지 체크
        for i in c:
            if i in must_have:
                vowel_cnt += 1
            else:
                consonant_cnt += 1

        if vowel_cnt < 1 or consonant_cnt < 2:
            continue

        # 정렬되어 있는지 확인
        sorted_cur = ''.join(sorted(c))
        if cur != sorted_cur:
            continue
        
        answer.append(cur)

    return answer     

if __name__ == "__main__":
    L, C = map(int, input().split())
    charset = input().split()
    charset = sorted(charset)
    # print(charset)

    ret = solution(charset)
    ret.sort()
    for elem in ret:
        print(elem)

☝ 팁

문제의 조건을 잘 확인하고, 검증하는 로직을 세우는 게 중요

profile
'당신을 한 줄로 소개해보세요'를 이 블로그로 대신 해볼까합니다.

0개의 댓글