목차

단어 변환

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호43163
문제명단어 변환
레벨Level 3
분류

BFS

시간복잡도O(n*l)
인풋사이즈n<=50, l<=10
사용한 언어Python
해결날짜2021/06/11
태그

고득점 Kit - DFS/BFS

풀이

코드

코드 1

"""Solution code for "Programmers 43163. 단어 변환".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/43163
- Solution link: http://www.teferi.net/ps/problems/programmers/43163
"""

from teflib import search


def solution(begin, target, words):

    def convertible_words(cur_word):
        for w in words:
            if sum(1 for c1, c2 in zip(w, cur_word) if c1 != c2) == 1):
                yield w

    dist = search.min_distances(convertible_words, begin, target)
    return dist.get(target, 0)

코드 2

"""Solution code for "Programmers 43163. 단어 변환".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/43163
- Solution link: http://www.teferi.net/ps/problems/programmers/43163
"""

import string
from teflib import search


def solution(begin, target, words):

    def convertible_words(cur_word):
        for i in range(len(cur_word)):
            left, right = cur_word[:i], cur_word[i+1:]
            for ch in string.ascii_lowercase:
                next_word = left + ch + right
                if next_word in word_set:
                    yield next_word

    word_set = set(words)
    dist = search.min_distances(convertible_words, begin, target)
    return dist.get(target, 0)