본문 바로가기

Coding test

99클럽 코테 스터디 33일차 TIL, 프로그래머스 / 단어 변환

🔑 오늘의 학습 키워드 bfs

🔗 문제링크 https://school.programmers.co.kr/learn/courses/30/lessons/43163

 

def solution(begin, target, words):
    # 못찾는 경우
    if target not in set(words):
        return 0
    answer = 0
    stack = [(begin,0)]
    visited = set()
    visited.add(begin)
    while stack:
        start,cnt = stack.pop()        
        for word in words:
            if word not in visited:
                wrong_cnt = 0
                is_diff_one = True
                for i,j in zip(start, word):
                    if i != j:
                        wrong_cnt += 1
                    if wrong_cnt > 1:
                        is_diff_one = False
                        break
                if is_diff_one:
                    if word == target:
                        return cnt + 1
                    stack.append((word,cnt + 1))
                    visited.add(word)

 

 



#99클럽 #코딩테스트준비 #개발자취업 #항해99 #TIL