Solve the Word Ladder problem using Backtracking and return the list of transformations from begin_word to end_word. Parameters: begin_word (str): The word from which the transformation starts. end_word (str): The target word for transformation. word_list (list[str]): The l
(begin_word: str, end_word: str, word_set: set[str])
| 66 | |
| 67 | |
| 68 | def word_ladder(begin_word: str, end_word: str, word_set: set[str]) -> list[str]: |
| 69 | """ |
| 70 | Solve the Word Ladder problem using Backtracking and return |
| 71 | the list of transformations from begin_word to end_word. |
| 72 | |
| 73 | Parameters: |
| 74 | begin_word (str): The word from which the transformation starts. |
| 75 | end_word (str): The target word for transformation. |
| 76 | word_list (list[str]): The list of valid words for transformation. |
| 77 | |
| 78 | Returns: |
| 79 | list[str]: The list of transformations from begin_word to end_word. |
| 80 | Returns an empty list if there is no valid transformation. |
| 81 | |
| 82 | Example: |
| 83 | >>> word_ladder("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]) |
| 84 | ['hit', 'hot', 'dot', 'lot', 'log', 'cog'] |
| 85 | |
| 86 | >>> word_ladder("hit", "cog", ["hot", "dot", "dog", "lot", "log"]) |
| 87 | [] |
| 88 | |
| 89 | >>> word_ladder("lead", "gold", ["load", "goad", "gold", "lead", "lord"]) |
| 90 | ['lead', 'lead', 'load', 'goad', 'gold'] |
| 91 | |
| 92 | >>> word_ladder("game", "code", ["came", "cage", "code", "cade", "gave"]) |
| 93 | ['game', 'came', 'cade', 'code'] |
| 94 | """ |
| 95 | |
| 96 | if end_word not in word_set: # no valid transformation possible |
| 97 | return [] |
| 98 | |
| 99 | # Perform backtracking starting from the begin_word |
| 100 | return backtrack(begin_word, [begin_word], end_word, word_set) |
nothing calls this directly
no test coverage detected