본문 바로가기
알고리즘/프로그래머스 level 3

[파이썬🐍] 프로그래머스 : 단어 변환

by 코딩개미뚠뚠 2021. 7. 20.
반응형
def solution(begin,target,words):
    answer = 0
    if target not in words: #찾고자 하는 단어가 words에 없을 경우
        return 0
    stack = []
    stack.append(begin)
    visited = []
    while stack:
        word = stack.pop()
        if word == target:
            return answer
        else:
            for i in range(len(words)):
                if len([j for j in range(len(words[i])) if words[i][j]!=word[j]]) == 1: #한글자만 다르다면
                    if words[i] not in visited:
                        visited.append(words[i])
                        stack.append(words[i])
        answer += 1
    return answer
반응형

댓글