Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions solutions/python/practice256.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
# Problem: Word Ladder
# Source: https://leetcode.com/problems/word-ladder/
# Note: Finds the length of the shortest transformation sequence between two words, changing one letter at a time using dictionary words.
# Status: 🔄 IN PROGRESS - Implementation needed
# TODO: Implement BFS for shortest transformation sequence
# Note: Implement BFS for shortest transformation sequence using deque
# Status: ✅ Done

def word_ladder(word):
pass
from collections import deque

dictionary = {"hot", "dot", "dog", "lot", "log", "cog"}
def word_ladder(beginWord, endWord, wordList):
wordSet = set(wordList)
if endWord not in wordSet:
return 0
queue = deque([(beginWord, 1)])
wordSet.discard(beginWord)

# BFS
while queue:
word, steps = queue.popleft()
if word == endWord:
return steps
for i in range(len(word)):
for character in "abcdefghijklmnopqrstuvwxyz":
if character == word[i]:
continue
newWord = word[:i] + character + word[i+1:]
if newWord in wordSet:
wordSet.remove(newWord)
queue.append((newWord, steps + 1))
return 0

# Example
dictionary = ["hot", "dot", "dog", "lot", "log", "cog"]
print(word_ladder("hit", "cog", dictionary)) # 5
Loading