융융이'Blog

단어 변환 본문

2022이전/알고리즘(하루에하나씩!)

단어 변환

바로퇴장 2020. 9. 11. 01:25
counts = []


def solution(begin, target, words):
    answer = 0
    visited = [0] * len(words)
    print(visited[0] == 0)

    if target in words:
        print("있음")
        find_array(begin, words, visited, 0, target)
        answer = print(min(counts))
    else:
        return answer
    # if last_check(words, target):

    return answer


def check(last, target):
    count = 0
    for i in range(len(target)):
        if last[i] == target[i]:
            count += 1
    if count == len(target) - 1:
        return True
    else:
        return False


def find_array(begin, words, visited, count, target):
    array_words = []
    for i in words:
        if check(begin, i) and visited[words.index(i)] == 0:
            array_words.append(i)
    if len(array_words) == 0 and begin == target:
        counts.append(count)
        print("끝")
    else:
        for i in array_words:
            visited[words.index(i)] = 1
            find_array(i, words, visited, count + 1, target)

solution("hit", "cog",["hot", "dot", "dog", "lot", "log", "cog"])

왜 런타임 에러가 뜨는지 참...