Word Ladder All Paths Problem
Word Ladder All Paths Problem — ExecCode Hard DSA Practice
Solve the Word Ladder All Paths problem on ExecCode. Free online hard DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Transform beginword into endword by changing one letter at a time. Every intermediate must be in the word list. Return every shortest transformation sequence (including begin and end). Sort the list of paths lexicographically. If impossible, return an empty list. You are given: beginword — starting word endword — goal word word_list — allowed intermediates plus possibly the end Return list of paths, each path a list of words. Sorted. Do not return longer-than-shortest ladders.
Examples
Input {'beginword': 'hit', 'endword': 'cog', 'wordlist': ['hot', 'dot', 'dog', 'lot', 'log', 'cog']}; Output [['hit', 'hot', 'dot', 'dog', 'cog'], ['hit', 'hot', 'lot', 'log', 'cog']]. Input {'beginword': 'hit', 'endword': 'cog', 'wordlist': ['hot', 'dot', 'dog', 'lot', 'log']}; Output []. Input {'beginword': 'a', 'endword': 'c', 'word_list': ['a', 'b', 'c']}; Output [['a', 'c'], ['a', 'c']]
Constraints
All words have the same length begin_word may or may not be in the list
Practice Word Ladder All Paths free on ExecCode. Browse DSA problems, topic map, and placement guides.