[프로그래머스/Python] 모음사전

Sujin Lee·2022년 11월 22일
2

코딩테스트

목록 보기
165/172
post-thumbnail

문제

프로그래머스 - 모음사전

해결 과정

  • DFS
    • word_list에 A, AA, AAA, AAAA, AAAAA을 넣으면서 DFS를 돌다가
    • cnt = 5일 때 되돌아가면 AAAAE, AAAAI, AAAAO, AAAAU를 넣다가
    • AAAE, AAAEA, AAAEI ... 를 넣게 된다. 이런 식으로 완전 탐색 진행
  • 우리가 찾는 word의 순서는 word_list에서 word의 인덱스 + 1

풀이

def solution(word):
    answer = 0
    word_list = []
    words = 'AEIOU'
    
    def dfs(cnt, w):
        if cnt == 5:
            return 
        for i in range(len(words)):
            word_list.append(w + words[i])
            dfs(cnt+1, w + words[i])
            
    dfs(0,"")
    
    return word_list.index(word)+1
profile
공부한 내용을 기록하는 공간입니다. 📝

0개의 댓글