프로그래머스 level3 단어변환 (Python)

Kim Yongbin·2023년 9월 8일
0

코딩테스트

목록 보기
63/162

Problem

https://school.programmers.co.kr/learn/courses/30/lessons/43163

Solution

def solution(begin, target, words):
    answer = 0
    
    if target not in words:
        return 0
    return dfs(begin, target, words, 0)

def get_next_words(curr, words):
    next_words = []
    for word in words:
        count = 0
        for i in range(len(curr)):
            if word[i] != curr[i]:
                count += 1
        if count == 1:
            next_words.append(word)
            
    return next_words

def dfs(curr, target, words, count):
    # end
    if curr == target:
        return count
    
    # action
    next_words = get_next_words(curr, words)
    
    # next
    min_count = 50
    for next_word in next_words:
        new_words = [word for word in words if word != next_word]
        res_count = dfs(next_word, target, new_words, count+1)
        min_count = min(res_count, min_count)
        
    return min_count

dfs를 이용하여 최솟값을 구하였다.

profile
반박 시 여러분의 말이 맞습니다.

0개의 댓글