ps | |
---|---|
링크 | programmers.co.kr/… |
출처 | 프로그래머스 |
문제 번호 | 43163 |
문제명 | 단어 변환 |
레벨 | Level 3 |
분류 |
BFS |
시간복잡도 | O(n*l) |
인풋사이즈 | n<=50, l<=10 |
사용한 언어 | Python |
해결날짜 | 2021/06/11 |
태그 |
"""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)
"""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)