|
| 1 | +""" |
| 2 | +Word Ladder is a classic problem in computer science. |
| 3 | +The problem is to transform a start word into an end word |
| 4 | +by changing one letter at a time. |
| 5 | +Each intermediate word must be a valid word from a given list of words. |
| 6 | +The goal is to find a transformation sequence |
| 7 | +from the start word to the end word. |
| 8 | +
|
| 9 | +Wikipedia: https://en.wikipedia.org/wiki/Word_ladder |
| 10 | +""" |
| 11 | + |
| 12 | +import string |
| 13 | + |
| 14 | + |
| 15 | +def backtrack( |
| 16 | + current_word: str, path: list[str], end_word: str, word_set: set[str] |
| 17 | +) -> list[str]: |
| 18 | + """ |
| 19 | + Helper function to perform backtracking to find the transformation |
| 20 | + from the current_word to the end_word. |
| 21 | +
|
| 22 | + Parameters: |
| 23 | + current_word (str): The current word in the transformation sequence. |
| 24 | + path (list[str]): The list of transformations from begin_word to current_word. |
| 25 | + end_word (str): The target word for transformation. |
| 26 | + word_set (set[str]): The set of valid words for transformation. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + list[str]: The list of transformations from begin_word to end_word. |
| 30 | + Returns an empty list if there is no valid |
| 31 | + transformation from current_word to end_word. |
| 32 | +
|
| 33 | + Example: |
| 34 | + >>> backtrack("hit", ["hit"], "cog", {"hot", "dot", "dog", "lot", "log", "cog"}) |
| 35 | + ['hit', 'hot', 'dot', 'lot', 'log', 'cog'] |
| 36 | +
|
| 37 | + >>> backtrack("hit", ["hit"], "cog", {"hot", "dot", "dog", "lot", "log"}) |
| 38 | + [] |
| 39 | +
|
| 40 | + >>> backtrack("lead", ["lead"], "gold", {"load", "goad", "gold", "lead", "lord"}) |
| 41 | + ['lead', 'lead', 'load', 'goad', 'gold'] |
| 42 | +
|
| 43 | + >>> backtrack("game", ["game"], "code", {"came", "cage", "code", "cade", "gave"}) |
| 44 | + ['game', 'came', 'cade', 'code'] |
| 45 | + """ |
| 46 | + |
| 47 | + # Base case: If the current word is the end word, return the path |
| 48 | + if current_word == end_word: |
| 49 | + return path |
| 50 | + |
| 51 | + # Try all possible single-letter transformations |
| 52 | + for i in range(len(current_word)): |
| 53 | + for c in string.ascii_lowercase: # Try changing each letter |
| 54 | + transformed_word = current_word[:i] + c + current_word[i + 1 :] |
| 55 | + if transformed_word in word_set: |
| 56 | + word_set.remove(transformed_word) |
| 57 | + # Recur with the new word added to the path |
| 58 | + result = backtrack( |
| 59 | + transformed_word, [*path, transformed_word], end_word, word_set |
| 60 | + ) |
| 61 | + if result: # valid transformation found |
| 62 | + return result |
| 63 | + word_set.add(transformed_word) # backtrack |
| 64 | + |
| 65 | + return [] # No valid transformation found |
| 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) |
0 commit comments