Files
baekjoon-study/workbook_8708/silver/4659-s5.py
2026-02-23 11:14:50 +09:00

65 lines
2.0 KiB
Python

# 비밀번호 발음하기
import sys
input = sys.stdin.readline
alphabet = "abcdefghijklmnopqrstuvwxyz"
VOWEL = "aeiou"
CONSONANT_OR_VOWEL = {a: 0 for a in alphabet} # 0이면 자음, 1이면 모음
for v in VOWEL:
CONSONANT_OR_VOWEL[v] = 1
def check_word(word):
is_vowel_in = False
sequence_count = [-1, 0] # sequence_count[0]은 자음(0),모음(1) 구분, sequence_count[1]은 count
before_w = ""
for w in word:
if not is_vowel_in and CONSONANT_OR_VOWEL[w] == 1: # 1번째 조건
is_vowel_in = True
if sequence_count[0] == CONSONANT_OR_VOWEL[w]: # 2번째 조건
sequence_count[1] += 1
if sequence_count[1] >= 3:
return False
else:
sequence_count[0], sequence_count[1] = CONSONANT_OR_VOWEL[w], 1
if before_w not in ("e", "o") and before_w == w: # 3번째 조건
return False
before_w = w
if not is_vowel_in: # 1번째 조건 최종 확인
return False
return True
def solution():
while 1:
word = input().rstrip()
if word == "end":
break
is_acceptable = check_word(word)
if is_acceptable:
print(f"<{word}> is acceptable.")
else:
print(f"<{word}> is not acceptable.")
return
solution()
"""
걸린 시간: 25분
시간 복잡도: 들어오는 모든 단어의 길이만큼의 시간이 걸린다.
해설: 자음과 모음 여부를 dictionary에 1, 0으로 O(1)에 구분할 수 있도록 세팅을 해둔다.
1번 조건은 한글자씩 볼 때 모음 여부에 따라 is_vowel_in 진위 여부를 결정한다.
2번 조건은 어떤 것이 몇 번 연속되고 있는지 현황을 기록하는 길이 2 리스트를 만들어서 판단한다. 기록과 다른 것이 나오면 초기화한다.
3번 조건은 이전 단어를 저장하는 변수를 만들고 이전 단어가 "e", "o"이면 통과하고, 아닐 경우 2번 연속되는지 확인한다.
"""