LV 3: 단어 변환

ewillwin·2023년 8월 29일
0

문제 링크

LV 3: 단어 변환


구현 방식

  • bfs로 풀어주었다
  • 시간 제한 조건이 빡빡하진 않아서 한 큐에 통과됐다

코드

from collections import deque

def solution(begin, target, words):
    N = len(begin)
    
    if target not in words:
        return 0
    
    queue = deque([]); queue.append((begin, 0))
    visit = set(); visit.add(begin)
    
    while queue:
        src, cnt = queue.popleft()
        if src == target:
            return cnt
        
        for word in words:
            check = 0
            for i in range(N):
                if word[i] == src[i]: check += 1
            if check == N-1:
                visit.add(word)
                queue.append((word, cnt+1))
    return 0
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글