from collections import deque

def solution(begin, target, words):
    if target not in words:
        return 0

    visited = [False] * len(words)
    queue = deque([(begin, 0)])

    def can_convert(a, b):
        diff = 0
        for x, y in zip(a, b):
            if x != y:
                diff += 1
        return diff == 1

    while queue:
        current, count = queue.popleft()

        if current == target:
            return count

        for i in range(len(words)):
            if not visited[i] and can_convert(current, words[i]):
                visited[i] = True
                queue.append((words[i], count + 1))

    return 0

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: