https://school.programmers.co.kr/learn/courses/30/lessons/84512
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. dfs 완전탐색 index 풀이
def solution(word):
dict = []
words = 'AEIOU'
def dfs(cnt, w):
if cnt == 5:
return
for i in range(len(words)):
dict.append(w + words[i])
dfs(cnt+1, w+words[i])
dfs(0, "")
return dict.index(word)+1
2. dfs 다른 풀이(index 안씀)
# 이렇게 푸는게 2배 이상 느림..
def solution(word):
global ans, idx
vowels = ['A','E','I','O','U']
ans = 0
idx = -1
def dfs(cnt, s):
global ans, idx
if cnt <= 5:
idx += 1
if s == word:
ans = idx
else:
return # 무한루프방지
for i in range(5):
dfs(cnt+1, s+vowels[i])
dfs(0,"")
return ans
'프로그래머스' 카테고리의 다른 글
프로그래머스 1844. 게임 맵 최단거리 (0) | 2025.01.17 |
---|---|
프로그래머스 43165. 타겟 넘버 (0) | 2025.01.15 |
프로그래머스 42839. 소수 찾기 (0) | 2025.01.14 |
프로그래머스 42579. 베스트앨범 (0) | 2025.01.14 |
프로그래머스 42578. 의상 (0) | 2025.01.09 |