[프로그래머스] 단어 변환

Narcoker·2024년 1월 8일
0

코딩테스트

목록 보기
143/152
post-custom-banner

문제

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

풀이

from collections import deque


def search(begin, target, words):
    answer = 0

    queue = deque([(begin, 0)])
    visited = [False] * len(words)

    while queue:
        cur_word, count = queue.popleft()
        for i, next_word in enumerate(words):
            if not visited[i] and can_change(cur_word, next_word):
                visited[i] = True
                queue.append((next_word, count + 1))

                if next_word == target:
                    return count + 1

    return answer


def can_change(cur_word, next_word):
    diff = 0

    for i in range(len(cur_word)):
        if cur_word[i] != next_word[i]:
            diff += 1
        if diff > 1:
            return False

    return True


def solution(begin, target, words):
    answer = 0

    if target in words:
        answer = search(begin, target, words)

    return answer

회고

zip을 사용한 풀이로 다시 풀어볼 것

profile
열정, 끈기, 집념의 Frontend Developer
post-custom-banner

0개의 댓글