forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from TheAlgorithms:master #51
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
jenningsloy318
merged 4 commits into
AlgorithmAndLeetCode:master
from
TheAlgorithms:master
Oct 3, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
918fa8b
Optimized O(n) to O(1) (#11669)
1227haran f4b4ac1
Adding Doctests to floyd_warshall.py (#11690)
alirashidAR 080e790
Add Word Break algorithm (#11687)
AswinPKumar01 40f65e8
Improve comments, docstrings in next_greatest_element.py (#11685)
JeevaRamanathan 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
71 changes: 71 additions & 0 deletions
backtracking/word_break.py
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,71 @@ | ||
| """ | ||
| Word Break Problem is a well-known problem in computer science. | ||
| Given a string and a dictionary of words, the task is to determine if | ||
| the string can be segmented into a sequence of one or more dictionary words. | ||
|
|
||
| Wikipedia: https://en.wikipedia.org/wiki/Word_break_problem | ||
| """ | ||
|
|
||
|
|
||
| def backtrack(input_string: str, word_dict: set[str], start: int) -> bool: | ||
| """ | ||
| Helper function that uses backtracking to determine if a valid | ||
| word segmentation is possible starting from index 'start'. | ||
|
|
||
| Parameters: | ||
| input_string (str): The input string to be segmented. | ||
| word_dict (set[str]): A set of valid dictionary words. | ||
| start (int): The starting index of the substring to be checked. | ||
|
|
||
| Returns: | ||
| bool: True if a valid segmentation is possible, otherwise False. | ||
|
|
||
| Example: | ||
| >>> backtrack("leetcode", {"leet", "code"}, 0) | ||
| True | ||
|
|
||
| >>> backtrack("applepenapple", {"apple", "pen"}, 0) | ||
| True | ||
|
|
||
| >>> backtrack("catsandog", {"cats", "dog", "sand", "and", "cat"}, 0) | ||
| False | ||
| """ | ||
|
|
||
| # Base case: if the starting index has reached the end of the string | ||
| if start == len(input_string): | ||
| return True | ||
|
|
||
| # Try every possible substring from 'start' to 'end' | ||
| for end in range(start + 1, len(input_string) + 1): | ||
| if input_string[start:end] in word_dict and backtrack( | ||
| input_string, word_dict, end | ||
| ): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| def word_break(input_string: str, word_dict: set[str]) -> bool: | ||
| """ | ||
| Determines if the input string can be segmented into a sequence of | ||
| valid dictionary words using backtracking. | ||
|
|
||
| Parameters: | ||
| input_string (str): The input string to segment. | ||
| word_dict (set[str]): The set of valid words. | ||
|
|
||
| Returns: | ||
| bool: True if the string can be segmented into valid words, otherwise False. | ||
|
|
||
| Example: | ||
| >>> word_break("leetcode", {"leet", "code"}) | ||
| True | ||
|
|
||
| >>> word_break("applepenapple", {"apple", "pen"}) | ||
| True | ||
|
|
||
| >>> word_break("catsandog", {"cats", "dog", "sand", "and", "cat"}) | ||
| False | ||
| """ | ||
|
|
||
| return backtrack(input_string, word_dict, 0) |
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
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
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.