-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add word ladder algorithm in backtracking #11590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tianyizheng02
merged 13 commits into
TheAlgorithms:master
from
Hardvan:wordladder_new_algo
Oct 2, 2024
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
00c2ec6
Add word ladder algorithm in backtracking
Hardvan 33fcf6c
Improve comments and implement ruff checks
Hardvan 34339f4
updating DIRECTORY.md
Hardvan 0c48c5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b64db78
Change BFS to Backtracking
Hardvan 96e16fc
Merge branch 'wordladder_new_algo' of https://github.com/Hardvan/Pyth…
Hardvan e0aca27
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e5d683f
Incorporate PR Changes
Hardvan 9154c3e
Resolve merge
Hardvan ec9fa0d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 131c430
Add type hints for backtrack function
Hardvan 211b272
Merge branch 'wordladder_new_algo' of https://github.com/Hardvan/Pyth…
Hardvan 59cadca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
Word Ladder is a classic problem in computer science. | ||
The problem is to transform a start word into an end word | ||
by changing one letter at a time. | ||
Each intermediate word must be a valid word from a given list of words. | ||
The goal is to find a transformation sequence | ||
from the start word to the end word. | ||
|
||
Wikipedia: https://en.wikipedia.org/wiki/Word_ladder | ||
""" | ||
|
||
|
||
def word_ladder_backtrack( | ||
Hardvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
begin_word: str, end_word: str, word_list: list[str] | ||
) -> list[str]: | ||
""" | ||
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 list of valid words for transformation. | ||
|
||
Returns: | ||
list[str]: The list of transformations from begin_word to end_word. | ||
Returns an empty list if there is no valid transformation. | ||
|
||
Example: | ||
>>> word_ladder_backtrack("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]) | ||
['hit', 'hot', 'dot', 'lot', 'log', 'cog'] | ||
|
||
>>> word_ladder_backtrack("hit", "cog", ["hot", "dot", "dog", "lot", "log"]) | ||
[] | ||
|
||
>>> word_ladder_backtrack("lead", "gold", ["load", "goad", "gold", "lead", "lord"]) | ||
['lead', 'lead', 'load', 'goad', 'gold'] | ||
|
||
>>> word_ladder_backtrack("game", "code", ["came", "cage", "code", "cade", "gave"]) | ||
['game', 'came', 'cade', 'code'] | ||
""" | ||
|
||
# Step 1: Convert the word_list to a set for faster lookup | ||
word_set = set(word_list) | ||
Hardvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Step 2: If end_word is not in the word_list, return an empty list | ||
if end_word not in word_set: | ||
return [] | ||
|
||
# Step 3: Backtracking function to find the word ladder | ||
def backtrack(current_word, path): | ||
Hardvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Base case: If the current word is the end word, return the path | ||
if current_word == end_word: | ||
return path | ||
|
||
# Try all possible single-letter transformations | ||
for i in range(len(current_word)): | ||
for c in "abcdefghijklmnopqrstuvwxyz": # Try changing each letter | ||
Hardvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transformed_word = current_word[:i] + c + current_word[i + 1 :] | ||
|
||
# If the transformed word is valid and has not been visited | ||
if transformed_word in word_set: | ||
# Remove it from the set to avoid revisiting | ||
word_set.remove(transformed_word) | ||
# Recur with the new word added to the path | ||
result = backtrack(transformed_word, [*path, transformed_word]) | ||
if result: # If we found a valid result, return it | ||
return result | ||
# Add it back to the set after exploring this path (backtrack) | ||
word_set.add(transformed_word) | ||
|
||
# If no valid path is found, return an empty list | ||
return [] | ||
|
||
# Step 4: Start backtracking from the begin_word | ||
return backtrack(begin_word, [begin_word]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.