Data Structures & Algorithms Graphs — BFS & DFS
💡
Exercise 115

Word Ladder 30 XP Hard

LeetCode Ctrl+Enter Run Ctrl+S Save

Find the shortest transformation sequence length from beginWord to endWord, changing exactly one letter per step. All intermediate words must be in the word list.

# hit -> hot -> dot -> dog -> cog = 5 steps

BFS on an implicit graph. Use wildcard patterns (h*t, *ot) to find neighbors efficiently.

💡 Pro tip: Understand this problem deeply — don't just memorize the code. Try explaining the approach out loud as if teaching a friend. If you can explain it simply, you truly understand it!

📋 Instructions
Find shortest transformation sequence length. Return 0 if impossible.
Build pattern map: for word 'hot', patterns are '*ot', 'h*t', 'ho*'. BFS from beginWord using pattern map.
🧪 Test Cases
Input
ladderLength('hit', 'cog', ['hot','dot','dog','lot','log','cog'])
Expected
5
Test case 1
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.