LV 2: 모음사전

ewillwin·2023년 8월 27일
0

문제 링크

LV 2: 모음사전


구현 방식

  • 규칙을 찾아보려 했으나 쉽지 않았다

  • 수가 증가하는 모양을 잘 보면, 재귀 형식임을 알 수가 있다

A
AA
AAA
AAAA
AAAAA
AAAAE
AAAAI
AAAAO
AAAAU
AAAE
AAAEA
...
  • 따라서 dfs로 완전탐색 해주었다
    -> count를 global로 선언하고 A부터 순서대로 dfs 호출하여 count 갱신. True가 반환된다면 바로 count 반환

코드

count = 0
def solution(word):
    global count
    vowels = ['A', 'E', 'I', 'O', 'U']
    
    def dfs(word_string):
        global count
        count += 1
        
        if word_string == word: return True #찾음
        elif len(word_string) == 5: return False
        
        for v in vowels:
            if dfs(word_string + v): return True # if문 반환값이 True면 바로 count 반환

    for v in vowels:
        if dfs(v): return count
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글

Powered by GraphCDN, the GraphQL CDN